首页

关于oschrenk-util源码包定义Systems类涉及java的系统环境实现源码示例

标签:oschrenk-util,Systems,java系统环境     发布时间:2018-07-20   

一、前言

关于oschrenk-util源码包中com.oschrenk.utils.Systems系统定义类,用于实现JAVA的系统环境判断及获取常用环境参数数据,详情参见源码说明部分。

二、源码说明

package com.oschrenk.utils;@b@@b@import java.io.File;@b@import java.io.PrintStream;@b@@b@public class Systems@b@{@b@  private static final String OS_NAME_WINDOWS_PREFIX = "Windows";@b@  private static final String USER_HOME_KEY = "user.home";@b@  private static final String USER_DIR_KEY = "user.dir";@b@  private static final String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir";@b@  private static final String JAVA_HOME_KEY = "java.home";@b@  public static final String AWT_TOOLKIT = getSystemProperty("awt.toolkit");@b@  public static final String FILE_ENCODING = getSystemProperty("file.encoding");@b@  public static final String FILE_SEPARATOR = getSystemProperty("file.separator");@b@  public static final String FILE_SEPARATOR_DOS = "\\";@b@  public static final String FILE_SEPARATOR_MAC = "/";@b@  public static final String FILE_SEPARATOR_UNIX = "/";@b@  public static final String JAVA_AWT_PRINTERJOB = getSystemProperty("java.awt.printerjob");@b@  public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path");@b@  public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version");@b@  public static final String JAVA_ENDORSED_DIRS = getSystemProperty("java.endorsed.dirs");@b@  public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs");@b@  public static final String JAVA_HOME = getSystemProperty("java.home");@b@  public static final String JAVA_IO_TMPDIR = getSystemProperty("java.io.tmpdir");@b@  public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path");@b@  public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name");@b@  public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version");@b@  public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name");@b@  public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor");@b@  public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version");@b@  public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = getSystemProperty("java.util.prefs.PreferencesFactory");@b@  public static final String JAVA_VENDOR = getSystemProperty("java.vendor");@b@  public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url");@b@  public static final String JAVA_VERSION = getSystemProperty("java.version");@b@  public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info");@b@  public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name");@b@  public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name");@b@  public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor");@b@  public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version");@b@  public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor");@b@  public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version");@b@  public static final String LINE_SEPARATOR = getSystemProperty("line.separator");@b@  public static final String LINE_SEPARATOR_DOS = "\r\n";@b@  public static final String LINE_SEPARATOR_MAC = "\r";@b@  public static final String LINE_SEPARATOR_UNIX = "\n";@b@  public static final String OS_ARCH = getSystemProperty("os.arch");@b@  public static final String OS_NAME = getSystemProperty("os.name");@b@  public static final String OS_VERSION = getSystemProperty("os.version");@b@  public static final String PATH_SEPARATOR = getSystemProperty("path.separator");@b@  public static final String PATH_SEPARATOR_DOS = ";";@b@  public static final String PATH_SEPARATOR_MAC = ":";@b@  public static final String PATH_SEPARATOR_UNIX = ":";@b@  public static final String USER_COUNTRY = (getSystemProperty("user.country") == null) ? getSystemProperty("user.region") : getSystemProperty("user.country");@b@  public static final String USER_DIR = getSystemProperty("user.dir");@b@  public static final String USER_HOME = getSystemProperty("user.home");@b@  public static final String USER_LANGUAGE = getSystemProperty("user.language");@b@  public static final String USER_NAME = getSystemProperty("user.name");@b@  public static final String USER_TIMEZONE = getSystemProperty("user.timezone");@b@  public static final String JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();@b@  public static final int JAVA_VERSION_INT = getJavaVersionAsInt();@b@  public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");@b@  public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");@b@  public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");@b@  public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");@b@  public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");@b@  public static final boolean IS_JAVA_1_6 = getJavaVersionMatches("1.6");@b@  public static final boolean IS_OS_AIX = getOSMatches("AIX");@b@  public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX");@b@  public static final boolean IS_OS_IRIX = getOSMatches("Irix");@b@  public static final boolean IS_OS_LINUX = (getOSMatches("Linux")) || (getOSMatches("LINUX"));@b@  public static final boolean IS_OS_MAC = getOSMatches("Mac");@b@  public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X");@b@  public static final boolean IS_OS_OS2 = getOSMatches("OS/2");@b@  public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris");@b@  public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS");@b@  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);@b@  public static final boolean IS_OS_WINDOWS = getOSMatches("Windows");@b@  public static final boolean IS_OS_WINDOWS_2000 = getOSMatches("Windows", "5.0");@b@  public static final boolean IS_OS_WINDOWS_95 = getOSMatches("Windows 9", "4.0");@b@  public static final boolean IS_OS_WINDOWS_98 = getOSMatches("Windows 9", "4.1");@b@  public static final boolean IS_OS_WINDOWS_ME = getOSMatches("Windows", "4.9");@b@  public static final boolean IS_OS_WINDOWS_NT = getOSMatches("Windows NT");@b@  public static final boolean IS_OS_WINDOWS_XP = getOSMatches("Windows", "5.1");@b@@b@  private static int getJavaVersionAsInt()@b@  {@b@    if (JAVA_VERSION_TRIMMED == null)@b@      return 0;@b@@b@    String str = JAVA_VERSION_TRIMMED.substring(0, 1);@b@    str = str + JAVA_VERSION_TRIMMED.substring(2, 3);@b@    if (JAVA_VERSION_TRIMMED.length() >= 5)@b@      str = str + JAVA_VERSION_TRIMMED.substring(4, 5);@b@    else@b@      str = str + "0";@b@    try@b@    {@b@      return Integer.parseInt(str); } catch (Exception ex) {@b@    }@b@    return 0;@b@  }@b@@b@  private static String getJavaVersionTrimmed()@b@  {@b@    int i;@b@    if (JAVA_VERSION != null)@b@      for (i = 0; i < JAVA_VERSION.length(); ++i) {@b@        char ch = JAVA_VERSION.charAt(i);@b@        if ((ch >= '0') && (ch <= '9'))@b@          return JAVA_VERSION.substring(i);@b@      }@b@@b@@b@    return null;@b@  }@b@@b@  private static boolean getJavaVersionMatches(String versionPrefix)@b@  {@b@    return ((JAVA_VERSION_TRIMMED != null) && (JAVA_VERSION_TRIMMED.startsWith(versionPrefix)));@b@  }@b@@b@  private static boolean getOSMatches(String osNamePrefix)@b@  {@b@    return ((OS_NAME != null) && (OS_NAME.startsWith(osNamePrefix)));@b@  }@b@@b@  private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix)@b@  {@b@    if ((OS_NAME == null) || (OS_VERSION == null))@b@      return false;@b@@b@    return ((OS_NAME.startsWith(osNamePrefix)) && (OS_VERSION.startsWith(osVersionPrefix)));@b@  }@b@@b@  private static String getSystemProperty(String property)@b@  {@b@    try@b@    {@b@      return System.getProperty(property);@b@    }@b@    catch (SecurityException ex) {@b@      System.err.println("Caught a SecurityException reading the system property '" + property + "'; the SystemUtils property value will default to null.");@b@    }@b@@b@    return null;@b@  }@b@@b@  public static boolean isJavaVersionAtLeast(int requiredVersion)@b@  {@b@    return (JAVA_VERSION_INT >= requiredVersion);@b@  }@b@@b@  public static File getJavaHome()@b@  {@b@    return new File(System.getProperty("java.home"));@b@  }@b@@b@  public static File getJavaIoTmpDir()@b@  {@b@    return new File(System.getProperty("java.io.tmpdir"));@b@  }@b@@b@  public static File getUserDir()@b@  {@b@    return new File(System.getProperty("user.dir"));@b@  }@b@@b@  public static File getUserHome()@b@  {@b@    return new File(System.getProperty("user.home"));@b@  }@b@}