一、前言
基于jdiary源码包中的定义com.l2fprod.util.OS系统类,可以识别当前JVM虚拟机环境(isWindows95/isWindowsNT/isWindows2000/isMacintosh/isSolaris/isLinux等)所在操作系统及JDK的版本。
二、源码说明
package com.l2fprod.util;@b@@b@import java.io.PrintStream;@b@@b@public final class OS@b@{@b@ public static final int JDK1_1 = 11;@b@ public static final int JDK1_2 = 12;@b@ public static final int JDK1_3 = 13;@b@ public static final int JDK1_4 = 14;@b@ private static boolean isWindows95;@b@ private static boolean isWindowsNT;@b@ private static boolean isWindows2000;@b@ private static boolean isMacintosh;@b@ private static boolean isSolaris;@b@ private static boolean isLinux;@b@ private static boolean isCaseSensitive;@b@ private static int jdkVersion = 11;@b@@b@ public static boolean isWindows()@b@ {@b@ return ((isWindows95()) || (isWindowsNT()) || (isWindows2000()));@b@ }@b@@b@ public static boolean isWindows95()@b@ {@b@ return isWindows95;@b@ }@b@@b@ public static boolean isWindowsNT()@b@ {@b@ return isWindowsNT;@b@ }@b@@b@ public static boolean isWindows2000()@b@ {@b@ return isWindows2000;@b@ }@b@@b@ public static boolean isMacintosh()@b@ {@b@ return isMacintosh;@b@ }@b@@b@ public static boolean isSolaris()@b@ {@b@ return isSolaris;@b@ }@b@@b@ public static boolean isLinux()@b@ {@b@ return isLinux;@b@ }@b@@b@ public static boolean isUnix()@b@ {@b@ return ((isSolaris()) || (isLinux()));@b@ }@b@@b@ public static boolean isCaseSensitive()@b@ {@b@ return isCaseSensitive;@b@ }@b@@b@ public static int getJDKVersion()@b@ {@b@ return jdkVersion;@b@ }@b@@b@ public static boolean isOneDotOne()@b@ {@b@ return (jdkVersion == 11);@b@ }@b@@b@ public static boolean isOneDotTwo()@b@ {@b@ return (jdkVersion == 12);@b@ }@b@@b@ public static boolean isOneDotThree()@b@ {@b@ return (jdkVersion == 13);@b@ }@b@@b@ public static boolean isOneDotFour()@b@ {@b@ return (jdkVersion == 14);@b@ }@b@@b@ public static boolean isOneDotFourOrMore() {@b@ return (jdkVersion >= 14);@b@ }@b@@b@ public static boolean isOneDotThreeOrMore()@b@ {@b@ return (jdkVersion >= 13);@b@ }@b@@b@ public static void openDocument(String path)@b@ throws Exception@b@ {@b@ if (isWindows2000()) {@b@ Runtime.getRuntime().exec(new String[] { "cmd /c start", path });@b@ }@b@ else if (isWindows()) {@b@ Runtime.getRuntime().exec(new String[] { "start", path });@b@ }@b@ else@b@ System.err.println("OS.openDocument() not supported on this platform (" + System.getProperty("os.name"));@b@ }@b@@b@ static@b@ {@b@ String s = System.getProperty("os.name").toLowerCase();@b@ String version = System.getProperty("os.version").toLowerCase();@b@@b@ if (("windows nt".equals(s)) && ("5.0".equals(version))) {@b@ isWindows2000 = true;@b@ }@b@ else if (s.equals("windows nt")) {@b@ isWindowsNT = true;@b@ }@b@ else if (s.startsWith("windows"))@b@ {@b@ isWindows95 = true;@b@ }@b@ else if ((s.equals("macintosh")) || (s.equals("macos")) || (s.equals("mac os")) || (s.equals("mac os x")))@b@ {@b@ isMacintosh = true;@b@ }@b@ else if ((s.equals("sunos")) || (s.equals("solaris"))) {@b@ isSolaris = true;@b@ isCaseSensitive = true;@b@ }@b@ else if (s.equals("linux")) {@b@ isLinux = true;@b@ isCaseSensitive = true;@b@ }@b@@b@ try@b@ {@b@ Class.forName("java.lang.ref.WeakReference");@b@ jdkVersion = 12;@b@ } catch (Exception e) {@b@ }@b@ try {@b@ Class.forName("javax.swing.UIDefaults$LazyInputMap");@b@ jdkVersion = 13;@b@ } catch (Exception e) {@b@ }@b@ try {@b@ Class.forName("java.lang.CharSequence");@b@ jdkVersion = 14;@b@ }@b@ catch (Exception e)@b@ {@b@ }@b@ }@b@}