Java 如何判断我是在 64 位 JVM 还是 32 位 JVM 中运行(从程序中)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2062020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?
提问by BobMcGee
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?
如何判断我的应用程序运行所在的 JVM 是 32 位还是 64 位?具体来说,我可以使用哪些函数或属性来在程序中检测到这一点?
采纳答案by codaddict
You retrieve the system propertythat marks the bitness of this JVM with:
您可以使用以下命令检索标记此 JVM 位数的系统属性:
System.getProperty("sun.arch.data.model");
Possible results are:
可能的结果是:
"32"
– 32-bit JVM"64"
– 64-bit JVM"unknown"
– Unknown JVM
"32"
– 32 位 JVM"64"
– 64 位 JVM"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".
编写Java代码时,如何区分32位和64位操作?
没有允许您区分 32 位和 64 位操作的公共 API。将 64 位视为一次编写、随处运行的传统平台。但是,如果您想编写特定于平台的代码(让您感到羞耻),则系统属性 sun.arch.data.model 具有值“32”、“64”或“未知”。
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.
一个可能需要这样做的示例是,如果您的 Java 代码依赖于本机库,并且您需要确定是在启动时加载 32 位还是 64 位版本的库。
回答by bryantsai
I installed 32-bit JVM and retried it again, looks like the following does tell you JVM bitness, not OS arch:
我安装了 32 位 JVM 并再次重试,看起来以下内容确实告诉您 JVM 位数,而不是操作系统架构:
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.
这已针对 SUN 和 IBM JVM(32 位和 64 位)进行了测试。显然,系统属性不仅仅是操作系统架构。
回答by gpampara
For certain versions of Java, you can check the bitness of the JVM from the command line with the flags -d32
and -d64
.
对于某些版本的 Java,您可以从命令行使用标志-d32
和-d64
.
$ java -help
...
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
To check for a 64-bit JVM, run:
要检查 64 位 JVM,请运行:
$ java -d64 -version
If it's not a 64-bit JVM, you'll get this:
如果它不是 64 位 JVM,你会得到这个:
Error: This Java instance does not support a 64-bit JVM.
Please install the desired version.
Similarly, to check for a 32-bit JVM, run:
同样,要检查 32 位 JVM,请运行:
$ java -d32 -version
If it's not a 32-bit JVM, you'll get this:
如果它不是 32 位 JVM,你会得到这个:
Error: This Java instance does not support a 32-bit JVM.
Please install the desired version.
These flags were added in Java 7, deprecatedin Java 9, removedin Java 10, and no longer available on modern versions of Java.
这些标志是在 Java 7 中添加的,在 Java 9 中已弃用,在 Java 10 中删除,并且在现代版本的 Java 中不再可用。
回答by Sedat Kilinc
Just type java -version
in your console.
只需java -version
在您的控制台中输入即可。
If a 64 bit version is running, you'll get a message like:
如果运行的是 64 位版本,您将收到如下消息:
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:
32 位版本将显示类似于:
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.
注意Client
而不是64-Bit Server
在第三行。这Client/Server
部分无关紧要,重要的是缺席64-Bit
。
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.
如果您的系统上安装了多个 Java 版本,请导航到您要检查的 Java 版本的 /bin 文件夹,然后java -version
在那里键入。
回答by dim
Complementary info:
补充资料:
On a running processyou may use (at least with some recent Sun JDK5/6 versions):
在您可能使用的正在运行的进程中(至少在某些最新的 Sun JDK5/6 版本中):
$ /opt/java1.5/bin/jinfo -sysprops 14680 | grep sun.arch.data.model
Attaching to process ID 14680, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 1.5.0_16-b02
sun.arch.data.model = 32
where 14680 is PID of jvm running the application. "os.arch" works too.
其中 14680 是运行应用程序的 jvm 的 PID。“os.arch”也有效。
Also other scenarios are supported:
还支持其他场景:
jinfo [ option ] pid
jinfo [ option ] executable core
jinfo [ option ] [server-id@]remote-hostname-or-IP
However consider also this note:
但是,请考虑以下注意事项:
"NOTE- This utility is unsupported and may or may not be available in future versions of the JDK. In Windows Systems where dbgent.dll is not present, 'Debugging Tools for Windows' needs to be installed to have these tools working. Also the PATH environment variable should contain the location of jvm.dll used by the target process or the location from which the Crash Dump file was produced."
"注意- 此实用程序不受支持,并且可能会或可能不会在 JDK 的未来版本中可用。在不存在 dbgent.dll 的 Windows 系统中,需要安装“Windows 调试工具”才能使这些工具正常工作。此外PATH 环境变量应包含目标进程使用的 jvm.dll 的位置或生成故障转储文件的位置。”
回答by jawsnnn
On Linux, you can get ELF header information by using either of the following two commands:
在 Linux 上,您可以使用以下两个命令之一获取 ELF 头信息:
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
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: Class: ELF64
o/p:等级:ELF 64
回答by Anthony Hayward
If you are using JNA, you can check whether com.sun.jna.Native.POINTER_SIZE == 4
(32 bit) or com.sun.jna.Native.POINTER_SIZE == 8
(64 bit).
如果您使用的是 JNA,您可以检查是com.sun.jna.Native.POINTER_SIZE == 4
(32 位)还是com.sun.jna.Native.POINTER_SIZE == 8
(64 位)。
回答by user1364368
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").
在 Windows 7下的“程序|程序和功能”下的“控制面板”中,JRE 和 JDK 的 64 位变体在括号中列出了“ 64 位”(例如“ Java SE Development Kit 7 Update 65(64 位) )"),而对于 32 位变体,括号中未提及该变体(例如仅“ Java SE Development Kit 8 Update 60”)。
回答by Olu Smith
To get the version of JVM currently running the program
获取当前运行程序的 JVM 版本
System.out.println(Runtime.class.getPackage().getImplementationVersion());
回答by BullyWiiPlaza
For Windows
, you can check the Java
home location. If it contains (x86)
it is 32-bit
otherwise 64-bit
:
对于Windows
,您可以检查Java
家庭位置。如果它包含(x86)
它,32-bit
否则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?
为什么要关心Windows
唯一的解决方案?
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.
如果您需要知道您正在运行的位版本,您可能会摆弄本机代码,Windows
因此平台独立性无论如何都是不可能的。