panda.lang.Systems Maven / Gradle / Ivy
Show all versions of panda-core Show documentation
package panda.lang;
import java.io.File;
import java.io.PrintWriter;
import panda.io.stream.StringBuilderWriter;
/**
*
* Helpers for {@code java.lang.System}.
*
*
* If a system property cannot be read due to security restrictions, the corresponding field in this class will be set
* to {@code null} and a message will be written to {@code System.err}.
*
*
* #ThreadSafe#
*
*
*/
public class Systems {
/**
* The prefix String for all Windows OS.
*/
private static final String OS_NAME_WINDOWS_PREFIX = "Windows";
// System property constants
// -----------------------------------------------------------------------
// These MUST be declared first. Other constants depend on this.
/**
* The System property key for the user home directory.
*/
private static final String USER_HOME_KEY = "user.home";
/**
* The System property key for the user directory.
*/
private static final String USER_DIR_KEY = "user.dir";
/**
* The System property key for the Java IO temporary directory.
*/
private static final String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir";
/**
* The System property key for the Java home directory.
*/
private static final String JAVA_HOME_KEY = "java.home";
/**
*
* The {@code awt.toolkit} System Property.
*
*
* Holds a class name, on Windows XP this is {@code sun.awt.windows.WToolkit}.
*
*
* On platforms without a GUI, this value is {@code null}.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*/
public static final String AWT_TOOLKIT = getProperty("awt.toolkit");
/**
*
* The {@code file.encoding} System Property.
*
*
* File encoding, such as {@code Cp1252}.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String FILE_ENCODING = getProperty("file.encoding");
/**
*
* The {@code file.separator} System Property. File separator ("/"
on
* UNIX).
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String FILE_SEPARATOR = getProperty("file.separator");
/**
*
* The {@code java.awt.fonts} System Property.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
*/
public static final String JAVA_AWT_FONTS = getProperty("java.awt.fonts");
/**
*
* The {@code java.awt.graphicsenv} System Property.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
*/
public static final String JAVA_AWT_GRAPHICSENV = getProperty("java.awt.graphicsenv");
/**
*
* The {@code java.awt.headless} System Property. The value of this property is the String
* {@code "true"} or {@code "false"}.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @see #isJavaAwtHeadless()
* @since Java 1.4
*/
public static final String JAVA_AWT_HEADLESS = getProperty("java.awt.headless");
/**
*
* The {@code java.awt.printerjob} System Property.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*/
public static final String JAVA_AWT_PRINTERJOB = getProperty("java.awt.printerjob");
/**
*
* The {@code java.class.path} System Property. Java class path.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String JAVA_CLASS_PATH = getProperty("java.class.path");
/**
*
* The {@code java.class.version} System Property. Java class format version number.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String JAVA_CLASS_VERSION = getProperty("java.class.version");
/**
*
* The {@code java.compiler} System Property. Name of JIT compiler to use. First in JDK version
* 1.2. Not used in Sun JDKs after 1.2.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2. Not used in Sun versions after 1.2.
*/
public static final String JAVA_COMPILER = getProperty("java.compiler");
/**
*
* The {@code java.endorsed.dirs} System Property. Path of endorsed directory or directories.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.4
*/
public static final String JAVA_ENDORSED_DIRS = getProperty("java.endorsed.dirs");
/**
*
* The {@code java.ext.dirs} System Property. Path of extension directory or directories.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.3
*/
public static final String JAVA_EXT_DIRS = getProperty("java.ext.dirs");
/**
*
* The {@code java.home} System Property. Java installation directory.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String JAVA_HOME = getProperty(JAVA_HOME_KEY);
/**
*
* The {@code java.io.tmpdir} System Property. Default temp file path.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_IO_TMPDIR = getProperty(JAVA_IO_TMPDIR_KEY);
/**
*
* The {@code java.library.path} System Property. List of paths to search when loading
* libraries.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_LIBRARY_PATH = getProperty("java.library.path");
/**
*
* The {@code java.runtime.name} System Property. Java Runtime Environment name.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.3
*/
public static final String JAVA_RUNTIME_NAME = getProperty("java.runtime.name");
/**
*
* The {@code java.runtime.version} System Property. Java Runtime Environment version.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.3
*/
public static final String JAVA_RUNTIME_VERSION = getProperty("java.runtime.version");
/**
*
* The {@code java.specification.name} System Property. Java Runtime Environment specification
* name.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_SPECIFICATION_NAME = getProperty("java.specification.name");
/**
*
* The {@code java.specification.vendor} System Property. Java Runtime Environment specification
* vendor.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_SPECIFICATION_VENDOR = getProperty("java.specification.vendor");
/**
*
* The {@code java.specification.version} System Property. Java Runtime Environment
* specification version.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*/
public static final String JAVA_SPECIFICATION_VERSION = getProperty("java.specification.version");
/**
* This is value is inilialized by {@link #JAVA_SPECIFICATION_VERSION}
* 1.1 -> 1
* 1.x -> x
* 1.8 -> 8
* 9 -> 9
* ..
* 12 -> 12
*/
public static final int JAVA_MAJOR_VERSION = getJavaMajorVersion();
/**
*
* The {@code java.util.prefs.PreferencesFactory} System Property. A class name.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.4
*/
public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = getProperty("java.util.prefs.PreferencesFactory");
/**
*
* The {@code java.vendor} System Property. Java vendor-specific string.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String JAVA_VENDOR = getProperty("java.vendor");
/**
*
* The {@code java.vendor.url} System Property. Java vendor URL.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String JAVA_VENDOR_URL = getProperty("java.vendor.url");
/**
*
* The {@code java.version} System Property. Java version number.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String JAVA_VERSION = getProperty("java.version");
/**
*
* The {@code java.vm.info} System Property. Java Virtual Machine implementation info.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_VM_INFO = getProperty("java.vm.info");
/**
*
* The {@code java.vm.name} System Property. Java Virtual Machine implementation name.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_VM_NAME = getProperty("java.vm.name");
/**
*
* The {@code java.vm.specification.name} System Property. Java Virtual Machine specification
* name.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_VM_SPECIFICATION_NAME = getProperty("java.vm.specification.name");
/**
*
* The {@code java.vm.specification.vendor} System Property. Java Virtual Machine specification
* vendor.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_VM_SPECIFICATION_VENDOR = getProperty("java.vm.specification.vendor");
/**
*
* The {@code java.vm.specification.version} System Property. Java Virtual Machine specification
* version.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_VM_SPECIFICATION_VERSION = getProperty("java.vm.specification.version");
/**
*
* The {@code java.vm.vendor} System Property. Java Virtual Machine implementation vendor.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_VM_VENDOR = getProperty("java.vm.vendor");
/**
*
* The {@code java.vm.version} System Property. Java Virtual Machine implementation version.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String JAVA_VM_VERSION = getProperty("java.vm.version");
/**
*
* The {@code line.separator} System Property. Line separator ("\n"
on
* UNIX).
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String LINE_SEPARATOR = getSystemLineSeparator();
private static String getSystemLineSeparator() {
String ls = getProperty("line.separator");
if (ls == null) {
// avoid security issues
StringBuilderWriter buf = new StringBuilderWriter(4);
PrintWriter out = new PrintWriter(buf);
out.println();
ls = buf.toString();
out.close();
}
return ls;
}
/**
*
* The {@code os.arch} System Property. Operating system architecture.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String OS_ARCH = getProperty("os.arch");
/**
* is 32bit OS
*/
public static final boolean IS_OS_32BIT = Strings.endsWith(OS_ARCH, "86");
/**
* is 64bit OS
*/
public static final boolean IS_OS_64BIT = Strings.endsWith(OS_ARCH, "64");
/**
*
* The {@code os.name} System Property. Operating system name.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String OS_NAME = getProperty("os.name");
/**
*
* The {@code os.version} System Property. Operating system version.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String OS_VERSION = getProperty("os.version");
/**
*
* The {@code user.country} or {@code user.region} System Property. User's country code, such as
* {@code GB}. First in Java version 1.2 as {@code user.region}. Renamed to {@code user.country}
* in 1.4
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String USER_COUNTRY = getProperty("user.country") == null ? getProperty("user.region") : getProperty("user.country");
/**
*
* The {@code user.dir} System Property. User's current working directory.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String USER_DIR = getProperty(USER_DIR_KEY);
/**
*
* The {@code user.home} System Property. User's home directory.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String USER_HOME = getProperty(USER_HOME_KEY);
/**
*
* The {@code user.language} System Property. User's language code, such as {@code "en"}.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.2
*/
public static final String USER_LANGUAGE = getProperty("user.language");
/**
*
* The {@code user.name} System Property. User's account name.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
* @since Java 1.1
*/
public static final String USER_NAME = getProperty("user.name");
/**
*
* The {@code user.timezone} System Property. For example: {@code "America/Los_Angeles"}.
*
*
* Defaults to {@code null} if the runtime does not have security access to read this property
* or the property does not exist.
*
*
* This value is initialized when the class is loaded. If
* {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the
* value will be out of sync with that System property.
*
*
*/
public static final String USER_TIMEZONE = getProperty("user.timezone");
// Operating system checks
// -----------------------------------------------------------------------
// These MUST be declared after those above as they depend on the
// values being set up
// OS names from http://www.vamphq.com/os.html
/**
*
* Is {@code true} if this is Android.
*
*
* The field will return {@code false} if Class: android.Manifest can not be loaded.
*
*/
public static final boolean IS_OS_ANDROID = (Classes.findClass("android.Manifest") != null);
/**
*
* Is {@code true} if this is AppEngine.
*
*
* The field will return {@code false} if Class: com.google.appengine.api.log.LogService can not be loaded.
*
*
*/
public static final boolean IS_OS_APPENGINE = Strings.isNotEmpty(getProperty("com.google.appengine.application.id"));
/**
*
* Is {@code true} if this is AIX.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_AIX = getOSMatchesName("AIX");
/**
*
* Is {@code true} if this is HP-UX.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_HP_UX = getOSMatchesName("HP-UX");
/**
*
* Is {@code true} if this is IBM OS/400.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
* @since 3.3
*/
public static final boolean IS_OS_400 = getOSMatchesName("OS/400");
/**
*
* Is {@code true} if this is Irix.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_IRIX = getOSMatchesName("Irix");
/**
*
* Is {@code true} if this is Linux.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_LINUX = getOSMatchesName("Linux")
|| getOSMatchesName("LINUX");
/**
*
* Is {@code true} if this is Mac.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_MAC = getOSMatchesName("Mac");
/**
*
* Is {@code true} if this is Mac.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_MAC_OSX = getOSMatchesName("Mac OS X");
/**
*
* Is {@code true} if this is Mac OS X Cheetah.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_CHEETAH = getOSMatches("Mac OS X", "10.0");
/**
*
* Is {@code true} if this is Mac OS X Puma.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_PUMA = getOSMatches("Mac OS X", "10.1");
/**
*
* Is {@code true} if this is Mac OS X Jaguar.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_JAGUAR = getOSMatches("Mac OS X", "10.2");
/**
*
* Is {@code true} if this is Mac OS X Panther.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
* @since 3.4
*/
public static final boolean IS_OS_MAC_OSX_PANTHER = getOSMatches("Mac OS X", "10.3");
/**
*
* Is {@code true} if this is Mac OS X Tiger.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_TIGER = getOSMatches("Mac OS X", "10.4");
/**
*
* Is {@code true} if this is Mac OS X Leopard.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_LEOPARD = getOSMatches("Mac OS X", "10.5");
/**
*
* Is {@code true} if this is Mac OS X Snow Leopard.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_SNOW_LEOPARD = getOSMatches("Mac OS X", "10.6");
/**
*
* Is {@code true} if this is Mac OS X Lion.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_LION = getOSMatches("Mac OS X", "10.7");
/**
*
* Is {@code true} if this is Mac OS X Mountain Lion.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_MOUNTAIN_LION = getOSMatches("Mac OS X", "10.8");
/**
*
* Is {@code true} if this is Mac OS X Mavericks.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_MAVERICKS = getOSMatches("Mac OS X", "10.9");
/**
*
* Is {@code true} if this is Mac OS X Yosemite.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_YOSEMITE = getOSMatches("Mac OS X", "10.10");
/**
*
* Is {@code true} if this is Mac OS X El Capitan.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_MAC_OSX_EL_CAPITAN = getOSMatches("Mac OS X", "10.11");
/**
*
* Is {@code true} if this is FreeBSD.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_FREE_BSD = getOSMatchesName("FreeBSD");
/**
*
* Is {@code true} if this is OpenBSD.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_OPEN_BSD = getOSMatchesName("OpenBSD");
/**
*
* Is {@code true} if this is NetBSD.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_NET_BSD = getOSMatchesName("NetBSD");
/**
*
* Is {@code true} if this is OS/2.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_OS2 = getOSMatchesName("OS/2");
/**
*
* Is {@code true} if this is Solaris.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_SOLARIS = getOSMatchesName("Solaris");
/**
*
* Is {@code true} if this is SunOS.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_SUN_OS = getOSMatchesName("SunOS");
/**
*
* Is {@code true} if this is a UNIX like system, as in any of AIX, HP-UX, Irix, Linux, MacOSX,
* Solaris or SUN OS.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_UNIX = IS_OS_AIX || IS_OS_HP_UX || IS_OS_IRIX || IS_OS_LINUX
|| IS_OS_MAC_OSX || IS_OS_SOLARIS || IS_OS_SUN_OS || IS_OS_FREE_BSD || IS_OS_OPEN_BSD
|| IS_OS_NET_BSD;
/**
*
* Is {@code true} if this is Windows.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS = getOSMatchesName(OS_NAME_WINDOWS_PREFIX);
/**
*
* Is {@code true} if this is Windows 2000.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_2000 = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " 2000");
/**
*
* Is {@code true} if this is Windows 2003.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_2003 = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " 2003");
/**
*
* Is {@code true} if this is Windows 2008.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_2008 = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " Server 2008");
/**
*
* Is {@code true} if this is Windows Server 2012.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_WINDOWS_2012 = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " Server 2012");
/**
*
* Is {@code true} if this is Windows 95.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_95 = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " 95");
/**
*
* Is {@code true} if this is Windows 98.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_98 = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " 98");
/**
*
* Is {@code true} if this is Windows ME.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_ME = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " Me");
/**
*
* Is {@code true} if this is Windows NT.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_NT = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " NT");
// Windows 2000 returns 'Windows 2000' but may suffer from same Java1.2 problem
/**
*
* Is {@code true} if this is Windows XP.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_XP = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " XP");
// -----------------------------------------------------------------------
/**
*
* Is {@code true} if this is Windows Vista.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_VISTA = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " Vista");
/**
*
* Is {@code true} if this is Windows 7.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*
*/
public static final boolean IS_OS_WINDOWS_7 = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " 7");
/**
*
* Is {@code true} if this is Windows 8.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_WINDOWS_8 = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " 8");
/**
*
* Is {@code true} if this is Windows 10.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
public static final boolean IS_OS_WINDOWS_10 = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " 10");
/**
*
* Is {@code true} if this is z/OS.
*
*
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
*
*/
// Values on a z/OS system I tested (Gary Gregory - 2016-03-12)
// os.arch = s390x
// os.encoding = ISO8859_1
// os.name = z/OS
// os.version = 02.02.00
public static final boolean IS_OS_ZOS = getOSMatchesName("z/OS");
// -----------------------------------------------------------------------
/**
*
* Gets the Java home directory as a {@code File}.
*
*
* @return a directory
* @throws SecurityException if a security manager exists and its {@code checkPropertyAccess}
* method doesn't allow access to the specified system property.
* @see System#getProperty(String)
*/
public static File getJavaHome() {
return new File(System.getProperty(JAVA_HOME_KEY));
}
/**
*
* Gets the Java IO temporary directory as a {@code File}.
*
*
* @return a directory
* @throws SecurityException if a security manager exists and its {@code checkPropertyAccess}
* method doesn't allow access to the specified system property.
* @see System#getProperty(String)
*/
public static File getJavaIoTmpDir() {
return new File(System.getProperty(JAVA_IO_TMPDIR_KEY));
}
/**
*
* Decides if the Java version matches.
*
*
* @param versionPrefix the prefix for the java version
* @return true if matches, or false if not or can't determine
*/
private static int getJavaMajorVersion() {
if (JAVA_SPECIFICATION_VERSION == null || JAVA_SPECIFICATION_VERSION.length() == 0) {
return 0;
}
try {
if (JAVA_SPECIFICATION_VERSION.length() >= 3 && JAVA_SPECIFICATION_VERSION.startsWith("1.")) {
return Integer.parseInt(JAVA_SPECIFICATION_VERSION.substring(2, 3));
}
return Integer.parseInt(JAVA_SPECIFICATION_VERSION);
}
catch (Exception e) {
System.err.println("Failed to getJavaMajorVersion for " + JAVA_SPECIFICATION_VERSION);
return 0;
}
}
/**
* Decides if the operating system matches.
*
* @param osNamePrefix the prefix for the os name
* @param osVersionPrefix the prefix for the version
* @return true if matches, or false if not or can't determine
*/
private static boolean getOSMatches(final String osNamePrefix, final String osVersionPrefix) {
return isOSMatch(OS_NAME, OS_VERSION, osNamePrefix, osVersionPrefix);
}
/**
* Decides if the operating system matches.
*
* @param osNamePrefix the prefix for the os name
* @return true if matches, or false if not or can't determine
*/
private static boolean getOSMatchesName(final String osNamePrefix) {
return isOSNameMatch(OS_NAME, osNamePrefix);
}
// -----------------------------------------------------------------------
/**
*
* Gets a System property, defaulting to {@code null} if the property cannot be read.
*
*
* If a {@code SecurityException} is caught, the return value is {@code null} and a message is
* written to {@code System.err}.
*
*
* @param name the system property name
* @return the system property value or {@code null} if a security problem occurs
*/
public static String getProperty(final String name) {
try {
return System.getProperty(name);
}
catch (final SecurityException ex) {
// we are not allowed to look at this property
System.err.println("Caught a SecurityException reading the system property '" + name + "'.");
return null;
}
}
/**
* Gets the system property indicated by the specified key.
* If a {@code SecurityException} is caught, the default value is returned and a message is
* written to {@code System.err}.
*
*
* @param name the system property name
* @param defv a default value.
*
* @return the system property value or the default if a security problem occurs
*/
public static String getProperty(final String name, final String defv) {
try {
return System.getProperty(name, defv);
}
catch (final SecurityException ex) {
// we are not allowed to look at this property
System.err.println("Caught a SecurityException reading the system property '" + name + "'.");
return defv;
}
}
/**
*
* Gets a environment variable, defaulting to {@code null} if the variable cannot be read.
*
*
* If a {@code SecurityException} is caught, the return value is {@code null} and a message is
* written to {@code System.err}.
*
*
* @param name the environment variable name
* @return the environment variable value or {@code null} if a security problem occurs
*/
public static String getenv(final String name) {
try {
return System.getenv(name);
}
catch (final SecurityException ex) {
// we are not allowed to look at this variable
System.err.println("Caught a SecurityException reading the environment variable '" + name + "'.");
return null;
}
}
/**
*
* Gets a environment variable, defaulting to {@code null} if the variable cannot be read.
*
*
* If a {@code SecurityException} is caught, the default value is returned and a message is
* written to {@code System.err}.
*
*
* @param name the environment variable name
* @param defv a default value.
* @return the environment variable value or the default value if a security problem occurs
*/
public static String getenv(final String name, final String defv) {
try {
String v = System.getenv(name);
return v == null ? defv : v;
}
catch (final SecurityException ex) {
// we are not allowed to look at this variable
System.err.println("Caught a SecurityException reading the environment variable '" + name + "'.");
return defv;
}
}
/**
*
* Gets the user directory as a {@code File}.
*
*
* @return a directory
* @throws SecurityException if a security manager exists and its {@code checkPropertyAccess}
* method doesn't allow access to the specified system property.
* @see System#getProperty(String)
*/
public static File getUserDir() {
return new File(System.getProperty(USER_DIR_KEY));
}
/**
*
* Gets the user home directory as a {@code File}.
*
*
* @return a directory
* @throws SecurityException if a security manager exists and its {@code checkPropertyAccess}
* method doesn't allow access to the specified system property.
* @see System#getProperty(String)
*/
public static File getUserHome() {
return new File(System.getProperty(USER_HOME_KEY));
}
/**
* Returns whether the {@link #JAVA_AWT_HEADLESS} value is {@code true}.
*
* @return {@code true} if {@code JAVA_AWT_HEADLESS} is {@code "true"}, {@code false} otherwise.
* @see #JAVA_AWT_HEADLESS
* @since Java 1.4
*/
public static boolean isJavaAwtHeadless() {
return Boolean.TRUE.toString().equals(JAVA_AWT_HEADLESS);
}
/**
* Decides if the operating system matches.
*
* This method is package private instead of private to support unit test invocation.
*
*
* @param osName the actual OS name
* @param osVersion the actual OS version
* @param osNamePrefix the prefix for the expected OS name
* @param osVersionPrefix the prefix for the expected OS version
* @return true if matches, or false if not or can't determine
*/
static boolean isOSMatch(final String osName, final String osVersion, final String osNamePrefix,
String osVersionPrefix) {
if (osName == null || osVersion == null) {
return false;
}
return isOSNameMatch(osName, osNamePrefix) && isOSVersionMatch(osVersion, osVersionPrefix);
}
/**
* Decides if the operating system matches.
*
* This method is package private instead of private to support unit test invocation.
*
*
* @param osName the actual OS name
* @param osNamePrefix the prefix for the expected OS name
* @return true if matches, or false if not or can't determine
*/
static boolean isOSNameMatch(final String osName, final String osNamePrefix) {
if (osName == null) {
return false;
}
return osName.startsWith(osNamePrefix);
}
/**
* Decides if the operating system version matches.
*
* This method is package private instead of private to support unit test invocation.
*
*
* @param osVersion the actual OS version
* @param osVersionPrefix the prefix for the expected OS version
* @return true if matches, or false if not or can't determine
*/
static boolean isOSVersionMatch(final String osVersion, final String osVersionPrefix) {
if (Strings.isEmpty(osVersion)) {
return false;
}
// Compare parts of the version string instead of using String.startsWith(String) because
// otherwise
// osVersionPrefix 10.1 would also match osVersion 10.10
final String[] versionPrefixParts = osVersionPrefix.split("\\.");
final String[] versionParts = osVersion.split("\\.");
for (int i = 0; i < Math.min(versionPrefixParts.length, versionParts.length); i++) {
if (!versionPrefixParts[i].equals(versionParts[i])) {
return false;
}
}
return true;
}
}