public class SysUtil
extends java.lang.Object
For example it is strongly recommended to uset exit(int)
rather
than System.exit(int)
to terminate a JVM. It allows registered
listeners to be notified just before the exit happens.
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
AIX |
static boolean |
DEBUG |
protected static java.lang.String |
DIGITAL_UNIX |
protected static java.lang.String |
FREE_BSD |
protected static java.lang.String |
HP_UX |
protected static java.lang.String |
IRIX |
static java.lang.String |
JAVA_HOME_DIR
Contains the value of system property "java.home" as set at startup.
|
protected static java.lang.String |
LINUX |
static java.lang.String |
OS_FAMILY_MACINTOSH
"macintosh"
|
static java.lang.String |
OS_FAMILY_UNIX
"unix"
|
static java.lang.String |
OS_FAMILY_UNKNOWN
"unknown"
|
static java.lang.String |
OS_FAMILY_WINDOWS
"windows"
|
protected static java.lang.String |
OS_PROPERTY_NAME |
protected static java.lang.String |
OSF1 |
protected static java.lang.String |
PF_OS_PROPERTY_NAME |
protected static java.lang.String |
SOLARIS |
protected static java.lang.String |
SUN_OS |
static SysUtil |
SYSU
An instance of this class which can be easily included
as static import and then used like
SYSU.getOsFamily() . |
protected static java.lang.String[] |
UNIX_SYSTEMS |
Modifier | Constructor and Description |
---|---|
protected |
SysUtil()
Initialize the new instance with default values.
|
Modifier and Type | Method and Description |
---|---|
void |
addSystemExitListener(ISystemExitListener listener)
Add the given listener to an internal list so that it would be called right
before system exit gets executed via the
exit(int) method. |
protected static boolean |
checkIfWindows() |
boolean |
close(java.io.Closeable closeable)
Close the given closeable ignoring any exception and checking for null before closing.
|
static SysUtil |
current()
Returns the only instance this class supports (design pattern "Singleton")
|
protected boolean |
determineIfEclipse()
Returns true if the environment is based on eclipse.
|
protected static java.lang.String |
determineOsFamilyName() |
protected static java.lang.String |
determineOsFamilyName(java.lang.String operationSystemName) |
ProcessExecutionResult |
executeExternal(java.io.File execDir,
java.lang.String command,
java.lang.String... args)
Executes the specified external command with the given arguments in the specified
execution directory and returns the combined result data in an object.
|
void |
exit(int rc)
Calling this method terminates the JVM.
|
void |
exit(int rc,
long sleepBeforeExit)
Calling this method terminates the JVM after the specified sleep time.
|
java.lang.String |
getConfigValue(java.lang.String sysPropName,
java.lang.String envVarName)
Returns the value of an external configuration setting by checking first
a system property and if that is not set an environment variable.
|
java.lang.String |
getConfigValue(java.lang.String sysPropName,
java.lang.String envVarName,
java.lang.String defaultValue)
Returns the value of an external configuration setting by checking first
a system property and if that is not set an environment variable and finally
if that's also not set returns the given default value.
|
protected java.util.List<ISystemExitListener> |
getExitListeners() |
int |
getJavaRuntimeVersion()
Returns the (normalized) version of the current Java Runtime.
|
java.lang.String |
getJdkBinaryDir()
Returns the path of the currently executing JDK's binary sub directory
or null if it is not found (e.g.
|
java.lang.String |
getOsFamily()
Returns the family name of the current operating system according to the
name specified in system property "os.name".
|
boolean |
isAppletEnvironment()
Returns true if the code is running as part of an applet in a browser
plugin JVM.
|
boolean |
isEclipse()
Returns true if the environment is based on eclipse.
|
boolean |
isExecutingWithJDK()
Returns true if the current Java execution runtime is a JDK
and false if it is a JRE.
|
boolean |
isJavaModuleSystemSupportingRuntime()
Returns true if the current JRE used to execute this code is
supporting the Java Module System.
|
boolean |
isWindows()
Returns true if the current operating-system is Windows.
|
protected void |
notifyExitListeners(int rc)
Inform all exit listeners that now the system will be exited.
|
void |
removeSystemExitListener(ISystemExitListener listener)
Removes the given listener from the internal list so that it would not
be notified anymore before system exit gets executed via the
exit(int)
method. |
protected void |
reset() |
protected void |
setExitListeners(java.util.List<ISystemExitListener> newValue) |
void |
sleep(long milliseconds)
Sets the current thread to sleep for the specified time in milliseconds
or until it gets interrupted.
|
public static final SysUtil SYSU
SYSU.getOsFamily()
.public static final boolean DEBUG
public static final java.lang.String OS_FAMILY_UNKNOWN
public static final java.lang.String OS_FAMILY_WINDOWS
public static final java.lang.String OS_FAMILY_UNIX
public static final java.lang.String OS_FAMILY_MACINTOSH
public static final java.lang.String JAVA_HOME_DIR
protected static final java.lang.String OS_PROPERTY_NAME
protected static final java.lang.String PF_OS_PROPERTY_NAME
protected static final java.lang.String SOLARIS
protected static final java.lang.String SUN_OS
protected static final java.lang.String LINUX
protected static final java.lang.String AIX
protected static final java.lang.String HP_UX
protected static final java.lang.String FREE_BSD
protected static final java.lang.String DIGITAL_UNIX
protected static final java.lang.String OSF1
protected static final java.lang.String IRIX
protected static final java.lang.String[] UNIX_SYSTEMS
public static SysUtil current()
protected static boolean checkIfWindows()
protected static java.lang.String determineOsFamilyName()
protected static java.lang.String determineOsFamilyName(java.lang.String operationSystemName)
public boolean isWindows()
public boolean isEclipse()
public boolean isAppletEnvironment()
public boolean isExecutingWithJDK()
public java.lang.String getJdkBinaryDir()
public java.lang.String getOsFamily()
OS_FAMILY_UNKNOWN
,
OS_FAMILY_UNIX
,
OS_FAMILY_MACINTOSH
,
OS_FAMILY_WINDOWS
public int getJavaRuntimeVersion()
public boolean isJavaModuleSystemSupportingRuntime()
public void sleep(long milliseconds)
milliseconds
- The time to sleeppublic void exit(int rc)
rc
- The return code to the program that started the JVM.public void exit(int rc, long sleepBeforeExit)
rc
- The return code to the program that started the JVM.sleepBeforeExit
- The time to sleep (in milliseconds) before exiting.public void addSystemExitListener(ISystemExitListener listener)
exit(int)
method.
listener
- The listener to add. Will be ignored if null.public void removeSystemExitListener(ISystemExitListener listener)
exit(int)
method.
listener
- The listener to remove. Will be ignored if null.public boolean close(java.io.Closeable closeable)
public ProcessExecutionResult executeExternal(java.io.File execDir, java.lang.String command, java.lang.String... args) throws java.io.IOException
execDir
- The directory in which the command execution should take place (may be null).command
- The name of the command or program to execute (must not be null).args
- An optional list of arguments to be passed to the external command.java.io.IOException
- In any case of problems with the file system.public java.lang.String getConfigValue(java.lang.String sysPropName, java.lang.String envVarName)
sysPropName
- The name of the system property to be checked (will be ignored if null).envVarName
- The name of the environment variable to be checked (will be ignored if null).public java.lang.String getConfigValue(java.lang.String sysPropName, java.lang.String envVarName, java.lang.String defaultValue)
sysPropName
- The name of the system property to be checked (will be ignored if null).envVarName
- The name of the environment variable to be checked (will be ignored if null).defaultValue
- The value to be returned if no value was found by the other two settings (may be null).protected void notifyExitListeners(int rc)
rc
- The return code for the exit.protected boolean determineIfEclipse()
protected void reset()
protected java.util.List<ISystemExitListener> getExitListeners()
protected void setExitListeners(java.util.List<ISystemExitListener> newValue)