最新的Windows操作系统中,System.getProperty("os.name")返回什么?

25

我的一些代码在x64上失效了,我开始深入挖掘,发现这是由于一些通过Runtime.getRuntime().exec()调用本机代码的代码造成的...

但是这段代码可能已经过时了,它没有考虑到更新的操作系统,并且有些代码看起来像这样:

String osName = System.getProperty("os.name");
    if (osName.equals("Windows NT") || osName.equals("Windows 2000") || osName.equals("Windows XP")) {
        cmd = new String[3];
        cmd[0] = WINDOWS_NT_2000_COMMAND_1;
        cmd[1] = WINDOWS_NT_2000_COMMAND_2;
        cmd[2] = command;
    } else if (osName.equals("Windows 95") || osName.equals("Windows 98") || osName.equalsIgnoreCase("Windows ME")) {
        cmd = new String[3];
        cmd[0] = WINDOWS_9X_ME_COMMAND_1;
        cmd[1] = WINDOWS_9X_ME_COMMAND_2;
        cmd[2] = command;

我想为所有新的操作系统(w2008, windows 7, ...)解决这个问题,但我没有访问每种类型主机的权限,也不想安装虚拟机来查看该值,是否有人知道某个列表的存在?目前为止我还没有找到。

编辑:我需要的是:Windows 7、Windows 2003、Windows 2008、Windows 2008R2。此外,我没有使用1.6u18,所以不用担心一些人提到的错误。


你可能会对这个问题感兴趣:https://dev59.com/wHI-5IYBdhLWcg3wio5k 不幸的是,我还没有时间将我的发现作为一个正式的开源项目发布 :( - sfussenegger
1
这不是一个答案,但这就是为什么你应该始终有一个默认情况。 - C. Ross
对于C. Ross来说,有一个else语句,但无论如何它都会失败,因为它默认认为它是Linux。 - Persimmonium
8个回答

13

虽然这不是一个完整的解决方案,但您可以获取32位JDK并使用不同兼容性设置运行简单的代码打印os.nameos.version

以下是在Windows 8.1上,不同的JDK报告的os.name/os.version值:

╔═════════════════╤════════════╤════════════╤════════════╤════════════╤═══════════════╤════════════════╤═════════════════════════════╤═══════════════════════════╤═════════════════════════════&#

6
很可能您可以更改代码,使其说:
if (osName.equals("Windows 95") || osName.equals("Windows 98") || osName.equalsIgnoreCase("Windows ME")) {
    cmd = new String[3];
    cmd[0] = WINDOWS_9X_ME_COMMAND_1;
    cmd[1] = WINDOWS_9X_ME_COMMAND_2;
    cmd[2] = command;
}
else {
    cmd = new String[3];
    cmd[0] = WINDOWS_NT_2000_COMMAND_1;
    cmd[1] = WINDOWS_NT_2000_COMMAND_2;
    cmd[2] = command;
}

3
我将测试这个小变化: 如果 (osName.equals("Windows 95")...) { //w95 etc} else if (osName.contains("Windows")){ //nt 和新的操作系统 } else {//unix} - Persimmonium

5
我曾在 Symantec 处理过这个问题,当时 Visual Cafe 还很流行... 我不建议以这种方式处理。问题在于不同的供应商可能提供不同的字符串。我建议使用特定于操作系统的方法来确定平台。
您可以在 Windows 上使用 "ver" 实用程序,在 Unix 类型系统上使用 "uname"。
在 Windows 上使用 "GetNativeSystemInfo" 可能更好,但那需要本地代码。
我建议使用这种方式而不是依赖 System.getProperty 方法的原因是因为您只需要处理底层操作系统,而不是坐在操作系统之上的 JVM - 这消除了不同 VM 为同一平台报告不同内容的问题。
编辑: 显然,您必须尝试不同的获取信息方式,因为其中一些可能需要运行 shell 而不仅仅是命令。但如果您坚持使用 bash,那么就应该没问题。基本上,请尝试运行命令,直到其中一个可行...虽然不太美观,但它会起作用。

2

由于新版本应该需要NT系列所需的内容,因此检查旧版本并使用NT设置可能更有意义,而不是检查新版本,类似于以下内容:

String osName = System.getProperty("os.name");
if (osName.equals("Windows 95") || osName.equals("Windows 98")
        || osName.equalsIgnoreCase("Windows ME")) {
    cmd = new String[3];
    cmd[0] = WINDOWS_9X_ME_COMMAND_1;
    cmd[1] = WINDOWS_9X_ME_COMMAND_2;
    cmd[2] = command;
} else {
    cmd = new String[3];
    cmd[0] = WINDOWS_NT_2000_COMMAND_1;
    cmd[1] = WINDOWS_NT_2000_COMMAND_2;
    cmd[2] = command;
}

2
没有列表,但在Windows7上,使用JDK6_u18:
os.name = “Windows 7”
注意:在JFK6_u14及之前版本中存在一个错误,它显示:
“Windows Vista”而不是“Windows 7”(即使实际上操作系统是“Windows 7”),所以要小心!
根据这个HowTo,W2003应该是“Windows 2003”。

2

bug_id=6819886... 这是我刚才提到的,只有3分钟前 ;) - VonC
2
唉,要是我在三分钟前就回复了,而不是为那个可怜的家伙搜索完整的值列表... - Jonathan Holloway
谢谢Jon,感激不尽。我已经看过那个列表了,你会发现新的操作系统都没有列出来... - Persimmonium

1
由于这在不同的搜索上下文中首次出现,我会为其他可能对这个主题进行更一般搜索的人提供以下信息:
我使用这个来确定广泛范围的操作系统(Windows vs Mac vs Linux等)。它是一个枚举,在程序的任何地方都可以利用不同的公共静态方法。 public enum OS {
WIN, MAC, LINUX, SOLARIS, FREEBSD;

private static OS thisOS;

private static void setThisOS() {
    String os = System.getProperty("os.name").toLowerCase();
    if (os.contains("win")) {
        thisOS = OS.WIN;
    }
    else if (os.contains("mac")) {
        thisOS = OS.MAC;
    }
    else if (os.contains("linux")) {
        thisOS = OS.LINUX;
    }
    else if (os.contains("sun")) {
        thisOS = OS.SOLARIS;
    }
    else if (os.contains("free")) {
        thisOS = OS.FREEBSD;
    }
}

public static boolean isWindows() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.WIN);
}

public static boolean isMac() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.MAC);
}

public static boolean isLinux() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.LINUX);
}

public static boolean isWinMac() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.WIN) || thisOS.equals(OS.MAC);
}

public static boolean isSun() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.SOLARIS);
}

public static boolean isBSD() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.FREEBSD);
}

public static boolean isNix() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.LINUX) || thisOS.equals(OS.SOLARIS) || thisOS.equals(OS.FREEBSD);
}

}


0

这段代码将会给你最新的Windows操作系统名称,例如“Windows Server 2016”。

public static String getFullOSName() {
        String cmds ="systeminfo";
        String osName = null;
        try {``
            BufferedReader bufferedreader = executeCommand(cmds);
            String line;
            while ((line = bufferedreader.readLine()) != null) {
                if (line.contains("OS Name")) {
                    String services[] = line.split(":");
                    osName = services[1].trim();
                    return osName;
                }
            }
        } catch (Exception ex) {
           }
        return osName;
    }

    /**
     * Execute Command 
     * 
     * @param command
     * @return
     * @throws Exception
     */

    private static BufferedReader executeCommand(String command) throws Exception {
        BufferedReader bufferedreader = null;
        try {
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec(command);
            InputStream inputstream = proc.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            bufferedreader = new BufferedReader(inputstreamreader);
        } catch (Exception ex) {
            throw new Exception("Command Execution failed on windows. command = " + command);
        }
        return bufferedreader;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接