如何判断我是在 64 位 JVM 还是 32 位 JVM 中运行(在程序内)?
如何判断应用程序运行的 JVM 是 32 位还是 64 位?具体来说,我可以使用哪些函数或属性来在程序中检测到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
如果你使用 JNA,你可以这样做
If you're using JNA, you can do this
这就是 JNA 使用
Platform.is64Bit()
解决此问题的方式(https://github.com/java-native-access/jna/blob/master/src/com/sun/jna/Platform.java )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)我相信以下内容,它使用 Foreign Function & Java 22 中添加的 Memory API 应该是一种可靠的方法。
ValueLayout.ADDRESS
:I believe the following, which uses the Foreign Function & Memory API added in Java 22, should be a reliable approach.
Documentation of
ValueLayout.ADDRESS
:您可以使用 JNI 库。这将始终有效并且独立于正在运行的 JVM 品牌。
Java 代码:
这是 C 代码:
You can use a JNI library. This will always work and is independent of the running JVM brand.
Java code:
And this is the C code:
在 Windows 7 中,“程序 | 程序和功能”下的“控制面板”中,JRE 和 JRE 的 64 位变体位于“程序 | 程序和功能”下的“控制面板”中。 JDK 列出时括号中带有“64 位”(例如“Java SE Development Kit 7 Update 65(64 位)”),而对于 32 位变体括号中未提及该变体(例如,仅“Java SE Development Kit 8 Update 60”)。
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").
获取当前运行程序的JVM版本
To get the version of JVM currently running the program
对于
Windows
,您可以检查Java
主位置。如果它包含(x86)
,则它是32位
,否则64位
:示例路径:
为什么关心
Windows
代码>唯一的解决方案?如果您需要知道正在运行的位版本,您可能会在
Windows
上摆弄本机代码,因此平台无关性无论如何都是不可能的。For
Windows
, you can check theJava
home location. If it contains(x86)
it is32-bit
otherwise64-bit
:Example paths:
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.对于某些版本的 Java,您可以使用标志
-d32
和-d64
从命令行检查 JVM 的位数。要检查 64 位 JVM,请运行:
如果它不是 64 位 JVM,您将得到以下信息:
同样,要检查 32 位 JVM,请运行:
如果它不是 32 位 JVM,您将得到以下结果:你会得到这样的信息:
这些标志是在 Java 7 中添加的,在 Java 中已弃用 9、在 Java 10 中已删除,并且在现代版本中不再可用爪哇。
For certain versions of Java, you can check the bitness of the JVM from the command line with the flags
-d32
and-d64
.To check for a 64-bit JVM, run:
If it's not a 64-bit JVM, you'll get this:
Similarly, to check for a 32-bit JVM, run:
If it's not a 32-bit JVM, you'll get this:
These flags were added in Java 7, deprecated in Java 9, removed in Java 10, and no longer available on modern versions of Java.
您检索标记此 JVM 位数的 系统属性 :
可能的结果是:
"32"
– 32 位 JVM"64"
– 64 位 JVM"unknown"
– 未知 JVM如上所述在热点常见问题解答中:
例如,如果您的 Java 代码依赖于本机库,并且您需要确定是在启动时加载 32 位版本还是 64 位版本的库,则可能需要这样做。
You retrieve the system property that marks the bitness of this JVM with:
Possible results are:
"32"
– 32-bit JVM"64"
– 64-bit JVM"unknown"
– Unknown JVMAs described in the HotSpot FAQ:
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 -version
即可。如果正在运行 64 位版本,您将收到如下消息:
32 位版本将显示类似以下内容:
注意
Client
而不是64-Bit Server
第三行。客户端/服务器
部分是无关紧要的,重要的是缺少64位
。如果您的系统上安装了多个 Java 版本,请导航到要检查的 Java 版本的 /bin 文件夹,然后在其中键入
java -version
。Just type
java -version
in your console.If a 64 bit version is running, you'll get a message like:
A 32 bit version will show something similar to:
Note
Client
instead of64-Bit Server
in the third line. TheClient/Server
part is irrelevant, it's the absence of the64-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.我安装了 32 位 JVM 并再次重试,看起来以下内容确实告诉您 JVM 位,而不是操作系统架构:
这是针对 SUN 和 IBM JVM(32 位和 64 位)进行测试的。显然,系统属性不仅仅是操作系统架构。
I installed 32-bit JVM and retried it again, looks like the following does tell you JVM bitness, not OS arch:
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 JDK5/6 版本):
其中 14680 是运行应用程序的 jvm 的 PID。 “os.arch”也有效。
还支持其他场景:
但是,还要考虑此注释:
“注意 - 此实用程序不受支持,并且在不存在 dbgent.dll 的 Windows 系统中可能可用,也可能不可用,需要安装“Windows 调试工具”才能使这些工具正常工作。变量应包含目标进程使用的 jvm.dll 的位置或生成故障转储文件的位置。”
Complementary info:
On a running process you may use (at least with some recent Sun JDK5/6 versions):
where 14680 is PID of jvm running the application. "os.arch" works too.
Also other scenarios are supported:
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."
如果您使用 JNA,则可以检查是否
com.sun.jna.Native.POINTER_SIZE == 4
(32 位)或com.sun.jna.Native.POINTER_SIZE == 8< /代码>(64 位)。
If you are using JNA, you can check whether
com.sun.jna.Native.POINTER_SIZE == 4
(32 bit) orcom.sun.jna.Native.POINTER_SIZE == 8
(64 bit).在Linux上,您可以使用以下两个命令之一获取ELF头信息:
o/p:
ELF 64 位 LSB 可执行文件,AMD x86-64,版本 1 (SYSV),适用于 GNU/Linux 2.4.0,动态链接(使用共享库),适用于 GNU/Linux 2.4.0,不剥离
或
o/p:
班级:ELF64
On Linux, you can get ELF header information by using either of the following two commands:
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
o/p:
Class: ELF64