public final class SystemUtil extends Object
java.lang.System
.Modifier and Type | Field and Description |
---|---|
static boolean |
IS_GENERIC_UNIX
|
static boolean |
IS_MAC_OS
|
static boolean |
IS_UNIX
|
static boolean |
IS_WINDOWS
|
static VersionNumber |
JAVA_9_0_0
Java v9.0.0.
|
static Platform |
PLATFORM
Returns the platform running this application:
Platform.WINDOWS ,
Platform.MAC_OS or Platform.GENERIC_UNIX . |
Modifier and Type | Method and Description |
---|---|
static int |
captureOutput(Process process,
Console console)
Captures output of specified newly started process (see
shellStart(String) ). |
static int |
captureOutput(Process process,
String[] capture)
Captures output of specified newly started process (see
shellStart(String) ). |
static File |
currentWorkingDir()
Returns the absolute, canonical directory corresponding to system
property user.dir (that is, the current working directory) if
this directory exists.
|
static String |
defaultEncoding()
Returns the default character encoding for this platform.
|
static File |
defaultUserPreferencesDir()
Equivalent to
userPreferencesDir("XMLmind", "XMLEditor10", "xxe10") . |
static File |
findAppInPath(String appName)
Searches specified application using the PATH
environment variable.
|
static boolean |
hasJavaFX()
Returns
true if the current Java runtime has a usable,
found by default in the CLASSPATH , JavaFX runtime. |
static File |
homeDir()
Returns the absolute, canonical directory corresponding to system
property user.home (that is, the home directory of the user of
this application) if this directory exists.
|
static VersionNumber |
javaVersionNumber()
Returns the version number of current JavaTM runtime.
|
static String[] |
listEncodings()
Returns the canonical names of all the charsets supported by
the Java runtime.
|
static VersionNumber |
osVersionNumber()
Returns the version number of the operating system.
|
static void |
setUserPreferencesDir(File dir)
Specify a custom user preferences directory.
|
static int |
shellExec(String command)
Equivalent to
shellExec(command, null, null, null) . |
static int |
shellExec(String command,
String[] capture)
Equivalent to
shellExec(command, null, null, capture) . |
static int |
shellExec(String command,
String[] envp,
File dir)
Equivalent to
shellExec(command, envp, dir, null) . |
static int |
shellExec(String command,
String[] envp,
File dir,
Console console)
Executes a command using the standard shell of the platform.
|
static int |
shellExec(String command,
String[] envp,
File dir,
String[] capture)
Executes a command using the standard shell of the platform, capturing
output to
System.out and System.err . |
static Process |
shellStart(String command)
Equivalent to
shellStart(command, null, null) . |
static Process |
shellStart(String command,
String[] envp,
File dir)
Executes a command using the standard shell of the platform.
|
static File |
userPreferencesDir()
Returns the user preferences directory of this application.
|
static File |
userPreferencesDir(String vendor,
String product,
String app)
Returns the directory used to store the preferences of specified
application.
|
public static final Platform PLATFORM
Platform.WINDOWS
,
Platform.MAC_OS
or Platform.GENERIC_UNIX
.public static final boolean IS_WINDOWS
public static final boolean IS_UNIX
public static final boolean IS_MAC_OS
public static final boolean IS_GENERIC_UNIX
public static final VersionNumber JAVA_9_0_0
public static File homeDir()
null
otherwise.public static File currentWorkingDir()
null
otherwise.public static void setUserPreferencesDir(File dir)
dir
- the custom user preferences directory
The fact that dir exists or is a directory is not checked.
Specify null
to revert to the default
user preferences directory of this application.
userPreferencesDir()
public static File userPreferencesDir()
setUserPreferencesDir(java.io.File)
.
defaultUserPreferencesDir()
public static File defaultUserPreferencesDir()
userPreferencesDir("XMLmind", "XMLEditor10", "xxe10")
.public static File userPreferencesDir(String vendor, String product, String app)
Example: vendor="XMLmind", product="XMLEditor", app="xxe":
vendor
- name of the vendor of the productproduct
- official product nameapp
- nickname of the productReturns null
if system property user.home is
not set or does not specify an existing directory.
The returned canonical file may specify a directory which does not exist yet and/or which may be impossible to create.
public static String defaultEncoding()
public static String[] listEncodings()
public static VersionNumber javaVersionNumber()
If the system property "java.version" does not exist or cannot be parsed, returns 1.8.0.
public static final boolean hasJavaFX()
true
if the current Java runtime has a usable,
found by default in the CLASSPATH
, JavaFX runtime.
public static VersionNumber osVersionNumber()
If the system property "os.version" does not exist or
cannot be parsed, returns null
.
public static Process shellStart(String command) throws IOException
shellStart(command, null, null)
.IOException
public static Process shellStart(String command, String[] envp, File dir) throws IOException
shellExec(java.lang.String, java.lang.String[])
, does not wait until the command is completed.command
- the shell command to be executedenvp
- array of pairs: environment variable name/value.
Specify null
to inherit the environment settings
of the current process.dir
- the working directory of the subprocess. Specify
null
to inherit the working directory of the current
process.IOException
- if an I/O error occurspublic static int shellExec(String command, String[] capture) throws IOException, InterruptedException
shellExec(command, null, null, capture)
.IOException
InterruptedException
public static int shellExec(String command, String[] envp, File dir, String[] capture) throws IOException, InterruptedException
System.out
and System.err
.command
- the shell command to be executedenvp
- array of pairs: environment variable name/value.
Specify null
to inherit the environment settings
of the current process.dir
- the working directory of the subprocess. Specify
null
to inherit the working directory of the current
process.capture
- output to System.out
is captured and saved
to capture[0]
and output to System.err
is
captured and saved to capture[1]
. May not be
null
.IOException
- if an I/O error occursInterruptedException
- if the current thread is interrupted by
another thread while it is waiting the completion of the shell commandpublic static int captureOutput(Process process, String[] capture) throws InterruptedException
shellStart(String)
).process
- newly started processcapture
- output to System.out
is captured and saved
to capture[0]
and output to System.err
is
captured and saved to capture[1]
. May not be
null
.InterruptedException
- if the current thread is interrupted by
another thread while it is waiting the completion of the processpublic static int shellExec(String command) throws IOException, InterruptedException
shellExec(command, null, null, null)
.IOException
InterruptedException
public static int shellExec(String command, String[] envp, File dir) throws IOException, InterruptedException
shellExec(command, envp, dir, null)
.IOException
InterruptedException
public static int shellExec(String command, String[] envp, File dir, Console console) throws IOException, InterruptedException
command
- the shell command to be executedenvp
- array of pairs: environment variable name/value.
Specify null
to inherit the environment settings
of the current process.dir
- the working directory of the subprocess. Specify
null
to inherit the working directory of the current
process.console
- output to System.out
is displayed by this
console as Console.MessageType.INFO
messages and output to
System.err
is is displayed by this console as Console.MessageType.ERROR
messages. May be null
.IOException
- if an I/O error occursInterruptedException
- if the current thread is interrupted by
another thread while it is waiting the completion of the shell commandpublic static int captureOutput(Process process, Console console) throws InterruptedException
shellStart(String)
).process
- newly started processconsole
- output to System.out
is captured and
displayed as INFO messages on this console and output to
System.err
is captured and sisplayed as ERROR messages on
this console. May be null
.InterruptedException
- if the current thread is interrupted by
another thread while it is waiting the completion of the processpublic static File findAppInPath(String appName)
appName
- the basename of the executable file of the application.
On Windows, this basename may have no extension. In such case, all the extensions found in the the PATHEXT environment variable (".exe", ".bat", etc), are tried in turn.
null
otherwise.