How can I tell if I'm running in 64-bit JVM or

2018-12-31 14:16发布

How can I tell if the JVM my application runs in is 32 bit or 64-bit? Specifically, what function or preference do I access to detect this within the program?

10条回答
几人难应
2楼-- · 2018-12-31 14:28

For Windows, you can check the Java home location. If it contains (x86) it is 32-bit otherwise 64-bit:

public static boolean is32Bit()
{
    val javaHome = System.getProperty("java.home");
    return javaHome.contains("(x86)");
}

public static boolean is64Bit()
{
    return !is32Bit();
}

Example paths:

C:\Program Files (x86)\Java\jdk1.8.0_181\bin\java.exe # 32-bit
C:\Program Files\Java\jdk-10.0.2\bin\java.exe # 64-bit

Why care about a Windows only solution?

If you need to know which bit version you're running on, you're likely fiddling around with native code on Windows so platform-independence is out of the window anyway.

查看更多
高级女魔头
3楼-- · 2018-12-31 14:29

Update Again:

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.

查看更多
孤独总比滥情好
4楼-- · 2018-12-31 14:34

Sun has a Java System property to determine the bitness of the JVM: 32 or 64:

sun.arch.data.model=32 // 32 bit JVM
sun.arch.data.model=64 // 64 bit JVM

You can use

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

to determine if its 32/64 from the program.

From the Sun 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".

The only good reason is if your java code is dependent upon native libraries and your code needs to determine which version (32 or 64bit) to load on startup.

查看更多
笑指拈花
5楼-- · 2018-12-31 14:34

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

查看更多
深知你不懂我心
6楼-- · 2018-12-31 14:41

Just type java -version in your console.

If a 64 bit version is running, you'll get a message like:

java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) 64-Bit Server VM (build 16.0-b13, mixed mode)

A 32 bit version will show something similar to:

java version "1.6.0_41"
Java(TM) SE Runtime Environment (build 1.6.0_41-b02)
Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode, sharing)

Note Client instead of 64-Bit Server in the third line. The Client/Server part is irrelevant, it's the absence of the 64-Bit that matters.

If multiple Java versions are installed on your system, navigate to the /bin folder of the Java version you want to check, and type java -version there.

查看更多
时光乱了年华
7楼-- · 2018-12-31 14:42

You can try on the command line:

java -d64 -version

If it's not a 64-bit version, you'll get a message that looks like:

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

Consult the help options of the JVM for more info java -help

查看更多
登录 后发表回答