【问题标题】:Java - Determine OS bit versionJava - 确定操作系统位版本
【发布时间】:2014-08-07 12:55:02
【问题描述】:

我使用以下问题的答案来确定系统位版本,除了在 mac osx 上也可以正常工作: How can I check the bitness of my OS using Java?? (J2SE, not os.arch)

String arch = System.getenv("PROCESSOR_ARCHITECTURE");
String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");

String realArch = arch.endsWith("64")
                  || wow64Arch != null && wow64Arch.endsWith("64")
                      ? "64" : "32";

最后一行 (realArch) 在 mac 上给了我一个 NPE,你知道我该如何解决它,我在 mac 上也得到了正确的位版本吗?

更新:

我看错了答案,抱歉。它在 windows、mac osx 和 ubuntu 上运行良好,只需稍作改动:

    String realArch = System.getProperty("os.arch").endsWith("64")
            ? "64" : "32";

    if (System.getProperty("os.name").startsWith("Windows")) {
        String arch = System.getenv("PROCESSOR_ARCHITECTURE");
        String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");
        realArch = arch.endsWith("64")
                || wow64Arch != null && wow64Arch.endsWith("64")
                ? "64" : "32";
    }

【问题讨论】:

    标签: java macos operating-system 64-bit


    【解决方案1】:

    您没有检查 arch 是否为 null:

    试试这个:

    String realArch = arch != null && arch.endsWith("64") || wow64Arch != null && wow64Arch.endsWith("64") ? "64" : "32";
    

    【讨论】:

      【解决方案2】:

      您使用的环境变量取决于操作系统,因此它们当然不会在所有平台上都有效。在 OS X 上尝试以下操作:

      public class Test {
      
          public static void main(String[] args) {
              System.out.println("Is 64Bit? " + is64BitMacOS());
          }
      
      
      
          public static boolean is64BitMacOS() {
              java.io.BufferedReader input = null;
              try {
                  String line;
                  Process proc = Runtime.getRuntime().exec("sysctl hw");
                  input = new java.io.BufferedReader(new java.io.InputStreamReader(proc.getInputStream()));
                  while ((line = input.readLine()) != null) {
                      if (line.length() > 0) {
                          if ((line.indexOf("cpu64bit_capable") != -1) && (line.trim().endsWith("1"))) {
                              return true;
                          }
                      }
                  }
              } catch (Exception ex) {
                  System.err.println(ex.getMessage());
              } finally {
                  try {
                      input.close();
                  } catch (Exception ex) {
                      System.err.println(ex.getMessage());
                  }
              }
      
              return false;
          }
      }
      

      【讨论】:

      • 我想我读错了另一个问题的答案。您知道 System.getProperty("os.arch") 是否适用于除 windows 之外的所有系统吗?
      • @user2693017 os.arch 为您提供 JVM 而不是操作系统的位数。如果你想知道 Java 是否是 64 位的,那么可以,使用os.arch。如果您需要了解底层操作系统的位数(不太可能,但如果说您正在使用一些本机库),请使用您的答案和我的答案中发布的代码。
      • @user2693017 而os.arch 适用于所有平台,包括Windows。
      • windows 如果程序使用 32bit java 运行,则返回 32,以避免兼容性问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 2017-04-22
      相关资源
      最近更新 更多