How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?

前端 未结 12 1795
滥情空心
滥情空心 2020-11-22 09:39

How can I tell if the JVM in which my application runs is 32 bit or 64-bit? Specifically, what functions or properties I can used to detect this within the program?

相关标签:
12条回答
  • 2020-11-22 10:12

    On Linux, you can get ELF header information by using either of the following two commands:

    file {YOUR_JRE_LOCATION_HERE}/bin/java
    

    o/p: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), for GNU/Linux 2.4.0, not stripped

    or

    readelf -h {YOUR_JRE_LOCATION_HERE}/bin/java | grep 'Class'
    

    o/p: Class: ELF64

    0 讨论(0)
  • 2020-11-22 10:19

    This is the way JNA solves this with Platform.is64Bit() (https://github.com/java-native-access/jna/blob/master/src/com/sun/jna/Platform.java)

     public static final boolean is64Bit() {
            String model = System.getProperty("sun.arch.data.model",
                                              System.getProperty("com.ibm.vm.bitmode"));
            if (model != null) {
                return "64".equals(model);
            }
            if ("x86-64".equals(ARCH)
                || "ia64".equals(ARCH)
                || "ppc64".equals(ARCH) || "ppc64le".equals(ARCH)
                || "sparcv9".equals(ARCH)
                || "mips64".equals(ARCH) || "mips64el".equals(ARCH)
                || "amd64".equals(ARCH)
                || "aarch64".equals(ARCH)) {
                return true;
            }
            return Native.POINTER_SIZE == 8;
    }
    
    ARCH = getCanonicalArchitecture(System.getProperty("os.arch"), osType);
    
    static String getCanonicalArchitecture(String arch, int platform) {
            arch = arch.toLowerCase().trim();
            if ("powerpc".equals(arch)) {
                arch = "ppc";
            }
            else if ("powerpc64".equals(arch)) {
                arch = "ppc64";
            }
            else if ("i386".equals(arch) || "i686".equals(arch)) {
                arch = "x86";
            }
            else if ("x86_64".equals(arch) || "amd64".equals(arch)) {
                arch = "x86-64";
            }
            // Work around OpenJDK mis-reporting os.arch
            // https://bugs.openjdk.java.net/browse/JDK-8073139
            if ("ppc64".equals(arch) && "little".equals(System.getProperty("sun.cpu.endian"))) {
                arch = "ppc64le";
            }
            // Map arm to armel if the binary is running as softfloat build
            if("arm".equals(arch) && platform == Platform.LINUX && isSoftFloat()) {
                arch = "armel";
            }
    
            return arch;
        }
    
    static {
            String osName = System.getProperty("os.name");
            if (osName.startsWith("Linux")) {
                if ("dalvik".equals(System.getProperty("java.vm.name").toLowerCase())) {
                    osType = ANDROID;
                    // Native libraries on android must be bundled with the APK
                    System.setProperty("jna.nounpack", "true");
                }
                else {
                    osType = LINUX;
                }
            }
            else if (osName.startsWith("AIX")) {
                osType = AIX;
            }
            else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) {
                osType = MAC;
            }
            else if (osName.startsWith("Windows CE")) {
                osType = WINDOWSCE;
            }
            else if (osName.startsWith("Windows")) {
                osType = WINDOWS;
            }
            else if (osName.startsWith("Solaris") || osName.startsWith("SunOS")) {
                osType = SOLARIS;
            }
            else if (osName.startsWith("FreeBSD")) {
                osType = FREEBSD;
            }
            else if (osName.startsWith("OpenBSD")) {
                osType = OPENBSD;
            }
            else if (osName.equalsIgnoreCase("gnu")) {
                osType = GNU;
            }
            else if (osName.equalsIgnoreCase("gnu/kfreebsd")) {
                osType = KFREEBSD;
            }
            else if (osName.equalsIgnoreCase("netbsd")) {
                osType = NETBSD;
            }
            else {
                osType = UNSPECIFIED;
            }
    }
    
    0 讨论(0)
  • 2020-11-22 10:20

    To get the version of JVM currently running the program

    System.out.println(Runtime.class.getPackage().getImplementationVersion());
    
    0 讨论(0)
  • 2020-11-22 10:23

    You retrieve the system property that marks the bitness of this JVM with:

    System.getProperty("sun.arch.data.model");
    

    Possible results are:

    • "32" – 32-bit JVM
    • "64" – 64-bit JVM
    • "unknown" – Unknown JVM

    As described in the HotSpot FAQ:

    When writing Java code, how do I distinguish between 32 and 64-bit operation?

    There's no public API that allows you to distinguish between 32 and 64-bit operation. Think of 64-bit as just another platform in the write once, run anywhere tradition. However, if you'd like to write code which is platform specific (shame on you), the system property sun.arch.data.model has the value "32", "64", or "unknown".

    An example where this could be necessary is if your Java code depends on native libraries, and you need to determine whether to load the 32- or 64-bit version of the libraries on startup.

    0 讨论(0)
  • 2020-11-22 10:23

    Under Windows 7 in the "Control Panel" under "Programs | Programs and Features" the 64-bit variants of JRE & JDK are listed with "64-bit" in parentheses (e.g. "Java SE Development Kit 7 Update 65 (64-Bit)"), while for the 32-bit variants the variant is not mentioned in parentheses (e.g. just "Java SE Development Kit 8 Update 60").

    0 讨论(0)
  • 2020-11-22 10:24

    I installed 32-bit JVM and retried it again, looks like the following does tell you JVM bitness, not OS arch:

    System.getProperty("os.arch");
    #
    # on a 64-bit Linux box:
    # "x86" when using 32-bit JVM
    # "amd64" when using 64-bit JVM
    

    This was tested against both SUN and IBM JVM (32 and 64-bit). Clearly, the system property is not just the operating system arch.

    0 讨论(0)
提交回复
热议问题