我如何知道我的应用程序运行的JVM是32位还是64位?具体来说,我可以在程序中使用哪些函数或属性来检测这一点?


当前回答

我安装了32位JVM,并再次重试,看起来以下确实告诉你JVM位,而不是OS arch:

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

这在SUN和IBM JVM(32和64位)上进行了测试。显然,系统属性不仅仅是操作系统拱门。

其他回答

在Linux上,您可以使用以下两个命令之一来获取ELF头信息:

file {YOUR_JRE_LOCATION_HERE}/bin/java

o / p: ELF 64位LSB可执行文件,AMD x86-64,版本1 (SYSV),适用于GNU/Linux 2.4.0,动态链接(使用共享库),适用于GNU/Linux 2.4.0,未剥离

or

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

o / p: 类 : ELF64

这是JNA用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;
        }
}

获取当前运行程序的JVM版本

System.out.println(Runtime.class.getPackage().getImplementationVersion());

对于某些版本的Java,您可以使用标记-d32和-d64从命令行检查JVM的位。

$ java -help
...
    -d32          use a 32-bit data model if available
    -d64          use a 64-bit data model if available

要检查64位JVM,运行:

$ java -d64 -version

如果它不是一个64位JVM,你会得到这个:

Error: This Java instance does not support a 64-bit JVM.
Please install the desired version.

类似地,要检查32位JVM,运行:

$ java -d32 -version

如果它不是一个32位JVM,你会得到这个:

Error: This Java instance does not support a 32-bit JVM.
Please install the desired version.

这些标志是在Java 7中添加的,在Java 9中已弃用,在Java 10中被删除,并且在Java的现代版本中不再可用。

您可以使用JNI库。这将始终工作,并且独立于运行的JVM品牌。

Java代码:

package org.mytest;

public class NativeBinding
{
    public static native int getRegisterWidth(); // returns 32 or 64
}

这是C代码:

#include <jni.h>

// will return register width (32 or 64)
extern "C" JNIEXPORT jint JNICALL
Java_org_mytest_NativeBinding_getRegisterWidth(JNIEnv*, jclass)
{
    return sizeof(void*) * 8;
}