我如何知道我的应用程序运行的JVM是32位还是64位?具体来说,我可以在程序中使用哪些函数或属性来检测这一点?
当前回答
在Windows 7的“控制面板”下的“程序|程序和功能”中,64位的JRE和JDK变体被列出,括号中有“64位”(例如:“Java SE Development Kit 7 Update 65(64位)”),而对于32位的变体,括号中没有提到该变体(例如,只是“Java SE Development Kit 8 Update 60”)。
其他回答
在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
只需在控制台中输入java -version。
如果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)
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)
注意第三行是客户端,而不是64位服务器。客户端/服务器部分无关紧要,重要的是缺少64位。
如果您的系统上安装了多个Java版本,请导航到要检查的Java版本的/bin文件夹,并在那里键入Java -version。
这是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;
}
}
您可以使用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;
}
获取当前运行程序的JVM版本
System.out.println(Runtime.class.getPackage().getImplementationVersion());
推荐文章
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象
- 在Java中保存最后N个元素的大小有限的队列
- 如何运行一个类从Jar不是主类在其清单文件