|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.util.OsUtil
public final class OsUtil
Provides static utility methods relating to the operating system that the JVM is running on.
This class is multithread safe: it is immutable (both its immediate state, as well as the deep state of its fields).
Nested Class Summary | |
---|---|
private static class |
OsUtil.ExecTask
Encapsulates all the work carried out by both the execSynch and execAsynch methods. |
static class |
OsUtil.UnitTest
See the Overview page of the project's javadocs for a general description of this unit test class. |
Field Summary | |
---|---|
private static String |
windows2000_osName
Official name of the Microsoft Windows 2000 operating system. |
private static String |
windows98_commandProcessor
Name of the command processor program to execute DOS commands under Windows 98 (and below). |
private static String |
windows98_osName
Official name of the Microsoft Windows 98 operating system. |
private static String |
windowsNt_commandProcessor
Name of the command processor program to execute DOS commands under Windows NT (and above). |
private static String |
windowsNt_osName
Official name of the Microsoft Windows NT operating system. |
private static String |
windowsXp_osName
Official name of the Microsoft Windows XP operating system. |
Constructor Summary | |
---|---|
private |
OsUtil()
This private constructor suppresses the default (public) constructor, ensuring non-instantiability. |
Method Summary | |
---|---|
static void |
execAsynch(String command)
Simply calls . |
static void |
execAsynch(String command,
String[] envp,
File dir)
Simply calls . |
static void |
execAsynch(String command,
String[] envp,
File dir,
StreamDrainer outDrainer,
StreamDrainer errDrainer)
Executes a native (operating system specific) command in a new child process. |
static byte[] |
execSynch(String command)
Returns . |
static byte[] |
execSynch(String command,
String[] envp,
File dir)
Returns . |
static byte[] |
execSynch(String command,
String[] envp,
File dir,
StreamDrainer outDrainer,
StreamDrainer errDrainer)
Executes a native (operating system specific) command in a new child process. |
private static boolean |
execSynchSucceeded(String command)
Calls execSynch (command). |
static String |
getCommandInvalid()
Returns a known invalid command for the current operating system. |
static String |
getCommandValid()
Returns a known valid command for the current operating system. |
static String |
getDosCommandProcessor()
Returns the name of the command processor program used to execute DOS commands if this is a Microsoft operating system. |
static String |
getOsName()
Returns the current operating system name. |
static String |
getOsNameAndVersion()
Returns a concatenation (with a space char separator) of the current operating system name and version. |
static String |
getOsVersion()
Returns the current operating system version. |
static boolean |
isMicrosoft()
Determines whether or not the operating system is from Microsoft (e.g. |
static boolean |
isUnix()
Determines whether or not the operating system is Unix (including Linux and OS/X). |
static boolean |
isWindows2000()
Determines whether or not the operating system is Windows 2000 by comparing windows2000_osName to the result of getOsName. |
static boolean |
isWindows98()
Determines whether or not the operating system is Windows 98 by comparing windows98_osName to the result of getOsName. |
static boolean |
isWindowsNT()
Determines whether or not the operating system is Windows NT by comparing windowsNt_osName to the result of getOsName. |
static boolean |
isWindowsXP()
Determines whether or not the operating system is Windows 2000 by comparing windowsXp_osName to the result of getOsName. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final String windows98_osName
private static final String windowsNt_osName
private static final String windows2000_osName
private static final String windowsXp_osName
private static final String windows98_commandProcessor
private static final String windowsNt_commandProcessor
Constructor Detail |
---|
private OsUtil()
Method Detail |
---|
public static String getCommandInvalid() throws IllegalStateException
IllegalStateException
- if unable to determine an invalid command for this operating systempublic static String getCommandValid() throws IllegalStateException
IllegalStateException
- if unable to determine a valid command for this operating systempublic static String getDosCommandProcessor() throws IllegalStateException
IllegalStateException
- if unable to determine what DOS command processor to use for this operating systempublic static String getOsName()
public static String getOsVersion()
public static String getOsNameAndVersion()
public static boolean isMicrosoft()
File.separatorChar
== '\\'getOsName
contains the text "windows" or "msdos" (case irrelevant)ver
was sucessfully executed
public static boolean isWindows98()
windows98_osName
to the result of getOsName.
public static boolean isWindowsNT()
windowsNt_osName
to the result of getOsName.
public static boolean isWindows2000()
windows2000_osName
to the result of getOsName.
public static boolean isWindowsXP()
windowsXp_osName
to the result of getOsName.
public static boolean isUnix()
File.separatorChar
== '/'uname
was sucessfully executed
private static boolean execSynchSucceeded(String command)
execSynch
(command).
Returns true if no Exception of any type was thrown, false otherwise.
Used by this class to determine valid/invalid commands.
public static byte[] execSynch(String command) throws IllegalArgumentException, RuntimeException
execSynch
(command, null, null)
.
So, the process which executes command inherits the env vars and working directory of the current Java process.
IllegalArgumentException
RuntimeException
public static byte[] execSynch(String command, String[] envp, File dir) throws IllegalArgumentException, RuntimeException
execSynch
(command, null, null, new StreamDrainerStoring
(), new StreamDrainerStoring())
.
In words:
Warning: because StreamDrainerStoring instances handle the process's std streams, the command must not generate excessive output over its lifetime else the program may run out of memory.
IllegalArgumentException
RuntimeException
public static byte[] execSynch(String command, String[] envp, File dir, StreamDrainer outDrainer, StreamDrainer errDrainer) throws IllegalArgumentException, RuntimeException
The command must not require any input, since this method does not know how to supply it with any; the command will hang forever waiting for input if you mistakenly call this method with such a command. This will cause the calling thread to block indefinately.
The command may, however, produce arbitrary output on either stream (its std out or err streams).
The outDrainer and errDrainer params will be initialized
with the child process's std out and err streams respectively,
and each will then be concurrently run in its own new thread.
This guarantees that those streams are properly drained.
command
- the operating system specific command to executeenvp
- array of strings, each element of which has environment variable settings in the format name=value,
or null if the subprocess should inherit the environment of the current process; see also
Accessing the Environment from Java Applicationsdir
- the working directory of the subprocess,
or null if the subprocess should inherit the working directory of the current processoutDrainer
- used to drain the child process's std out stream; its init
must not have been callederrDrainer
- used to drain the child process's std err stream; its init
must not have been called
StreamDrainer
for more discussion); if those bytes were not stored, then an empty (but never null) byte[] is returned
IllegalArgumentException
- if command is blank;
envp is not null but has a blank element;
dir is not null but is not valid
;
outDrainer is null;
errDrainer is null
RuntimeException
- if the native process terminates abnormally or wrote any data to its error stream,
or if some other error was detected (will be stored in the cause field of this RuntimeException)public static void execAsynch(String command) throws IllegalArgumentException, RuntimeException
execAsynch
(command, null, null)
.
So, the process which executes command inherits the env vars and working directory of the current Java process.
IllegalArgumentException
RuntimeException
public static void execAsynch(String command, String[] envp, File dir) throws IllegalArgumentException, RuntimeException
execAsynch
(command, null, null, new StreamDrainerStoring
(), new StreamDrainerStoring())
.
In words:
default Logger
when it terminatesWarning: because StreamDrainerStoring instances handle the process's std streams, the command must not generate excessive output over its lifetime else the program may run out of memory.
IllegalArgumentException
RuntimeException
public static void execAsynch(String command, String[] envp, File dir, StreamDrainer outDrainer, StreamDrainer errDrainer) throws IllegalArgumentException
The command must not require any input, since this method does not know how to supply it with any; the command will hang forever waiting for input if you mistakenly call this method with such a command. This will cause the background thread to block indefinately.
The command may, however, produce arbitrary output on either stream (its std out or err streams).
The outDrainer and errDrainer params will be initialized
with the child process's std out and err streams respectively,
and each will then be concurrently run in its own new thread.
This guarantees that those streams are properly drained.
The background thread ensures that process's outcome is always logged to the default Logger
when it terminates.
command
- the operating system specific command to executeenvp
- array of strings, each element of which has environment variable settings in the format name=value,
or null if the subprocess should inherit the environment of the current process; see also
Accessing the Environment from Java Applicationsdir
- the working directory of the subprocess,
or null if the subprocess should inherit the working directory of the current processoutDrainer
- used to drain the child process's std out stream; its init
must not have been callederrDrainer
- used to drain the child process's std err stream; its init
must not have been called
IllegalArgumentException
- if command is blank;
envp is not null but has a blank element;
dir is not null but is not valid
;
outDrainer is null;
errDrainer is null
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |