com.xmlmind.util
Class SystemUtil

java.lang.Object
  extended by com.xmlmind.util.SystemUtil

public final class SystemUtil
extends java.lang.Object

A collection of utility functions (static methods) which complements what's found in java.lang.System.


Field Summary
static boolean HAS_JAVAFX
          true if the current Java runtime has a usable, found by default in the CLASSPATH, JavaFX runtime.
static boolean IS_APPLE_JAVA
          true if the current Java runtime has been developed by Apple (that is, runs on Mac OS X and Java version is less than JAVA_1_7_0); false if it has been developed by Sun/Oracle.
static boolean IS_GENERIC_UNIX
          true if PLATFORM is Platform.GENERIC_UNIX.
static boolean IS_MAC_OS
          true if PLATFORM is Platform.MAC_OS.
static boolean IS_OLD_JAVA
          true if the current Java version is less than JAVA_1_8_0); false otherwise.
static boolean IS_UNIX
          true if PLATFORM is not Platform.WINDOWS.
static boolean IS_WINDOWS
          true if PLATFORM is Platform.WINDOWS.
static VersionNumber JAVA_1_7_0
          Java v1.7.0.
static VersionNumber JAVA_1_8_0
          Java v1.8.0.
static VersionNumber JAVA_1_8_0_40
          Java v1.8.0_40.
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.
 
Method Summary
static int captureOutput(java.lang.Process process, Console console)
          Captures output of specified newly started process (see shellStart(String)).
static int captureOutput(java.lang.Process process, java.lang.String[] capture)
          Captures output of specified newly started process (see shellStart(String)).
static java.io.File currentWorkingDir()
          Returns the absolute, canonical directory corresponding to system property user.dir (that is, the current working directory) if this directory exists.
static java.lang.String defaultEncoding()
          Returns the default character encoding for this platform.
static java.io.File defaultUserPreferencesDir()
          Equivalent to userPreferencesDir("XMLmind", "XMLEditor7", "xxe7").
static java.io.File findAppInPath(java.lang.String appName)
          Searches specified application using the PATH environment variable.
static java.io.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 java.lang.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(java.io.File dir)
          Specify a custom user preferences directory.
static int shellExec(java.lang.String command)
          Equivalent to shellExec(command, null, null, null).
static int shellExec(java.lang.String command, java.lang.String[] capture)
          Equivalent to shellExec(command, null, null, capture).
static int shellExec(java.lang.String command, java.lang.String[] envp, java.io.File dir)
          Equivalent to shellExec(command, envp, dir, null).
static int shellExec(java.lang.String command, java.lang.String[] envp, java.io.File dir, Console console)
          Executes a command using the standard shell of the platform.
static int shellExec(java.lang.String command, java.lang.String[] envp, java.io.File dir, java.lang.String[] capture)
          Executes a command using the standard shell of the platform, capturing output to System.out and System.err.
static java.lang.Process shellStart(java.lang.String command)
          Equivalent to shellStart(command, null, null).
static java.lang.Process shellStart(java.lang.String command, java.lang.String[] envp, java.io.File dir)
          Executes a command using the standard shell of the platform.
static java.io.File userPreferencesDir()
          Returns the user preferences directory of this application.
static java.io.File userPreferencesDir(java.lang.String vendor, java.lang.String product, java.lang.String app)
          Returns the directory used to store the preferences of specified application.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PLATFORM

public static final Platform PLATFORM
Returns the platform running this application: Platform.WINDOWS, Platform.MAC_OS or Platform.GENERIC_UNIX.


IS_WINDOWS

public static final boolean IS_WINDOWS
true if PLATFORM is Platform.WINDOWS.


IS_UNIX

public static final boolean IS_UNIX
true if PLATFORM is not Platform.WINDOWS.


IS_MAC_OS

public static final boolean IS_MAC_OS
true if PLATFORM is Platform.MAC_OS.


IS_GENERIC_UNIX

public static final boolean IS_GENERIC_UNIX
true if PLATFORM is Platform.GENERIC_UNIX.


JAVA_1_7_0

public static final VersionNumber JAVA_1_7_0
Java v1.7.0. See IS_APPLE_JAVA.


JAVA_1_8_0

public static final VersionNumber JAVA_1_8_0
Java v1.8.0.


JAVA_1_8_0_40

public static final VersionNumber JAVA_1_8_0_40
Java v1.8.0_40. See HAS_JAVAFX.


JAVA_9_0_0

public static final VersionNumber JAVA_9_0_0
Java v9.0.0.


IS_APPLE_JAVA

public static final boolean IS_APPLE_JAVA
true if the current Java runtime has been developed by Apple (that is, runs on Mac OS X and Java version is less than JAVA_1_7_0); false if it has been developed by Sun/Oracle.


IS_OLD_JAVA

public static final boolean IS_OLD_JAVA
true if the current Java version is less than JAVA_1_8_0); false otherwise.


HAS_JAVAFX

public static final boolean HAS_JAVAFX
true if the current Java runtime has a usable, found by default in the CLASSPATH, JavaFX runtime.

This is true as of JAVA_1_8_0_40.

Windows XP is not considered to have JavaFX because JDK 1.8 is not officially supported on it.

Method Detail

homeDir

public static java.io.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. Returns null otherwise.


currentWorkingDir

public static java.io.File currentWorkingDir()
Returns the absolute, canonical directory corresponding to system property user.dir (that is, the current working directory) if this directory exists. Returns null otherwise.


setUserPreferencesDir

public static void setUserPreferencesDir(java.io.File dir)
Specify a custom user preferences directory.

Parameters:
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.

See Also:
userPreferencesDir()

userPreferencesDir

public static java.io.File userPreferencesDir()
Returns the user preferences directory of this application. More precisely:

See Also:
defaultUserPreferencesDir()

defaultUserPreferencesDir

public static java.io.File defaultUserPreferencesDir()
Equivalent to userPreferencesDir("XMLmind", "XMLEditor7", "xxe7").


userPreferencesDir

public static java.io.File userPreferencesDir(java.lang.String vendor,
                                              java.lang.String product,
                                              java.lang.String app)
Returns the directory used to store the preferences of specified application.

Example: vendor="XMLmind", product="XMLEditor", app="xxe":

Parameters:
vendor - name of the vendor of the product
product - official product name
app - nickname of the product
Returns:
the user preferences directory.

Returns 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.


defaultEncoding

public static java.lang.String defaultEncoding()
Returns the default character encoding for this platform.

Returns:
canonical name of default charset for this platform

listEncodings

public static java.lang.String[] listEncodings()
Returns the canonical names of all the charsets supported by the Java runtime. The returned array is sorted.


javaVersionNumber

public static VersionNumber javaVersionNumber()
Returns the version number of current JavaTM runtime.

If the system property "java.version" does not exist or cannot be parsed, returns 1.6.0.


osVersionNumber

public static VersionNumber osVersionNumber()
Returns the version number of the operating system.

If the system property "os.version" does not exist or cannot be parsed, returns null.


shellStart

public static java.lang.Process shellStart(java.lang.String command)
                                    throws java.io.IOException
Equivalent to shellStart(command, null, null).

Throws:
java.io.IOException

shellStart

public static java.lang.Process shellStart(java.lang.String command,
                                           java.lang.String[] envp,
                                           java.io.File dir)
                                    throws java.io.IOException
Executes a command using the standard shell of the platform. Unlike shellExec(java.lang.String, java.lang.String[]), does not wait until the command is completed.

Parameters:
command - the shell command to be executed
envp - 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.
Returns:
the process of the shell
Throws:
java.io.IOException - if an I/O error occurs

shellExec

public static int shellExec(java.lang.String command,
                            java.lang.String[] capture)
                     throws java.io.IOException,
                            java.lang.InterruptedException
Equivalent to shellExec(command, null, null, capture).

Throws:
java.io.IOException
java.lang.InterruptedException

shellExec

public static int shellExec(java.lang.String command,
                            java.lang.String[] envp,
                            java.io.File dir,
                            java.lang.String[] capture)
                     throws java.io.IOException,
                            java.lang.InterruptedException
Executes a command using the standard shell of the platform, capturing output to System.out and System.err.

Parameters:
command - the shell command to be executed
envp - 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.
Returns:
the exit status returned by the shell
Throws:
java.io.IOException - if an I/O error occurs
java.lang.InterruptedException - if the current thread is interrupted by another thread while it is waiting the completion of the shell command

captureOutput

public static int captureOutput(java.lang.Process process,
                                java.lang.String[] capture)
                         throws java.lang.InterruptedException
Captures output of specified newly started process (see shellStart(String)).

Parameters:
process - newly started 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.
Returns:
exit status of process
Throws:
java.lang.InterruptedException - if the current thread is interrupted by another thread while it is waiting the completion of the process

shellExec

public static int shellExec(java.lang.String command)
                     throws java.io.IOException,
                            java.lang.InterruptedException
Equivalent to shellExec(command, null, null, null).

Throws:
java.io.IOException
java.lang.InterruptedException

shellExec

public static int shellExec(java.lang.String command,
                            java.lang.String[] envp,
                            java.io.File dir)
                     throws java.io.IOException,
                            java.lang.InterruptedException
Equivalent to shellExec(command, envp, dir, null).

Throws:
java.io.IOException
java.lang.InterruptedException

shellExec

public static int shellExec(java.lang.String command,
                            java.lang.String[] envp,
                            java.io.File dir,
                            Console console)
                     throws java.io.IOException,
                            java.lang.InterruptedException
Executes a command using the standard shell of the platform.

Parameters:
command - the shell command to be executed
envp - 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.
Returns:
the exit status returned by the shell
Throws:
java.io.IOException - if an I/O error occurs
java.lang.InterruptedException - if the current thread is interrupted by another thread while it is waiting the completion of the shell command

captureOutput

public static int captureOutput(java.lang.Process process,
                                Console console)
                         throws java.lang.InterruptedException
Captures output of specified newly started process (see shellStart(String)).

Parameters:
process - newly started process
console - 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.
Returns:
exit status of process
Throws:
java.lang.InterruptedException - if the current thread is interrupted by another thread while it is waiting the completion of the process

findAppInPath

public static java.io.File findAppInPath(java.lang.String appName)
Searches specified application using the PATH environment variable.

Parameters:
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.

Returns:
the absolute filename of the application if found; null otherwise.