代码之家  ›  专栏  ›  技术社区  ›  Persimmonium

在最新的Windows操作系统中,system.getproperty(“os.name”)返回什么

  •  20
  • Persimmonium  · 技术社区  · 15 年前

    我的一些代码在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,…)修复这个问题,但是我不能访问每种类型的主机,我也不想安装在一个vm中只是为了看到它的价值,有人知道某个地方的列表吗?还没有找到。

    编辑:我需要:Windows7、Windows2003、Windows2008、Windows2008R2 另外,我不是1.6U18,所以不用担心一些人提到的bug。

    7 回复  |  直到 8 年前
        1
  •  6
  •   erikkallen    15 年前

    很可能你会把代码改成

    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
  •  6
  •   Bass    10 年前

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

    这里是 OS.名称 / OS.版本 上不同JDK报告的值 视窗8.1 盒子:

    ╔═════════════════╤════════════╤════════════╤════════════╤═══════════════╤═══════════════╤══════════════════════╤══════════════════════╗
    ║ Java/OS version │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows 8            │ Windows 8.1          ║
    ╟─────────────────┼────────────┼────────────┼────────────┼───────────────┼───────────────┼──────────────────────┼──────────────────────╢
    ║ 1.4.2           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows Vista │ Windows Vista        │ Windows Vista        ║
    ║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.2 ║
    ║ 1.5.0           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows NT (unknown) │ Windows NT (unknown) ║
    ║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.2 ║
    ║ 1.6.0           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows 8            │ Windows 8            ║
    ║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.2 ║
    ║ 1.7.0           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows 8            │ Windows 8.1          ║
    ║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.3 ║
    ║ 1.8.0           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows 8            │ Windows 8.1          ║
    ║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.3 ║
    ╚═════════════════╧════════════╧════════════╧════════════╧═══════════════╧═══════════════╧══════════════════════╧══════════════════════╝
    
        3
  •  5
  •   TofuBeer    15 年前

    当visual cafe还活着的时候我在赛门铁克处理过这个…我完全不建议这样做。问题是不同的供应商可以提供不同的字符串。我建议使用特定于操作系统的方法来确定平台。

    您可以在windows上使用“ver”实用程序,在unix类型的系统上使用“uname”。

    在windows上使用“getnativesysteminfo”可能更好,但这需要本机代码。

    我建议这样做,而不是依赖于system.getproperty方法,因为这样您只需处理底层操作系统,而不必处理位于操作系统顶部的jvm,这样就消除了不同vm为同一平台报告不同内容的问题。

    编辑: 显然,您需要尝试不同的方法来获取信息,因为其中一些方法可能需要运行shell而不仅仅是命令。但如果你坚持猛击,那应该是好的。基本上试着运行命令直到其中一个工作…不漂亮,但会有用的。

        4
  •  2
  •   VonC    15 年前

    没有列表,但在Windows7上,使用JDK6_18:

    os.name =“Windows 7”

    注意:有一个 bug on JFK6_u14 and before ,其中显示:

    “WindowsVista”而不是“Windows7”(尽管操作系统实际上是“Windows7”),所以要小心!

    根据 this HowTo ,w2003应该是“windows 2003”。

        5
  •  2
  •   Fabian Steeg    15 年前

    由于较新版本需要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;
    }
    
        6
  •  2
  •   Jonathan Holloway    15 年前

    取决于你正在运行的Java版本,我遇到了这个bug:

    http://bugs.sun.com/view_bug.do?bug_id=6819886

    所以只要使用JDK的后一个版本,它就应该返回 视窗7 很好。

    但我不确定WindowsServer2008 Windows服务器2008 .

    这里有一个相当完整的列表:

    http://mindprod.com/jgloss/properties.html#OSNAME

        7
  •  0
  •   User42 Zohab Ali    8 年前

    此代码将为您提供最新的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;
        }