【问题标题】:what does System.getProperty("os.name") return in latest Windows OSsSystem.getProperty("os.name") 在最新的 Windows 操作系统中返回什么
【发布时间】:2010-03-01 17:38:10
【问题描述】:

我的一些代码在 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 中只是为了查看价值,有人知道某处的一些清单吗?还没找到。

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

【问题讨论】:

  • 您可能对这个问题感兴趣:stackoverflow.com/questions/1803075/… 不幸的是,我还没有时间将我的发现作为一个合适的开源项目发布:(
  • 这不是一个答案,但这就是为什么你应该总是有一个默认情况。
  • 致 C. Ross,有一个 else 语句,但无论如何它都失败了,因为它默认认为它是一个 linux

标签: java windows


【解决方案1】:

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

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

╔═════════════════╤════════════╤════════════╤═════ ═══════╤═══════════════╤═══════════════╤══════════ ════════════╤══════════════════════╗ ║ Java/OS 版本 │ 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 (未知) │ Windows NT (未知) ║ ║ │ 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 ║ ╚═════════════════╧════════════╧════════════╧═════ ═══════╧═══════════════╧═══════════════╧══════════ ════════════╧══════════════════════╝

【讨论】:

    【解决方案2】:

    您很可能可以将代码更改为说

    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;
    }
    

    【讨论】:

    • 我将测试这个的一个小变化: if (osName.equals("Windows 95")...){ //w95 etc} else if (osName.contains("Windows") ){ //nt 和新的} else {//unix}
    【解决方案3】:

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

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

    在 Windows 上使用“GetNativeSystemInfo”可能会更好,但这需要本地代码。

    我建议采用这种方式而不是依赖 System.getProperty 方法的原因是,您只需处理底层操作系统,而不是位于操作系统之上的 JVM - 这消除了不同 VM 报告的问题同一个平台不同的东西。

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

    【讨论】:

      【解决方案4】:

      没有列表,但在 Windows7 上,带有 JDK6_u18:

      os.name = "Windows 7"

      注意:有一个bug on JFK6_u14 and before,它显示在哪里:

      “Windows Vista”而不是“Windows 7”(即使操作系统实际上是“Windows 7”),所以要小心!

      根据this HowTo,W2003 应该是“Windows 2003”。

      【讨论】:

        【解决方案5】:

        由于较新的版本应该需要 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】:

          取决于您运行的 Java 版本,我遇到了这个错误:

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

          所以只要您使用 JDK 的较新版本,它应该返回 Windows 7 就好了。

          虽然不确定 Windows Server 2008,但我猜 Windows Server 2008

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

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

          【讨论】:

          • bug_id=6819886... 我刚刚在 3 分钟前提到过 ;)
          • sigh 如果我在 3 分钟前添加我的回复,而不是为可怜的家伙搜索完整的值列表...
          • 谢谢乔恩,感激不尽。我已经看过该列表,因为您看到缺少较新的操作系统...
          【解决方案7】:

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

          【讨论】:

            猜你喜欢
            • 2015-09-03
            • 2014-03-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-25
            • 2011-04-11
            • 2014-04-14
            • 2011-11-17
            相关资源
            最近更新 更多