1. Overview
1.概述
Although Java is platform-independent, there are times when we have to use native libraries. In those cases, we might need to identify the underlying platform and load the appropriate native libraries on startup.
尽管Java是独立于平台的,但有时我们也不得不使用本地库。在这些情况下,我们可能需要识别底层平台并在启动时加载适当的本地库。
In this tutorial, we’ll learn different ways to check if a Java program is running on a 64-bit or 32-bit JVM.
在本教程中,我们将学习不同的方法来检查Java程序是否在64位或32位JVM上运行。
First, we’ll show how to achieve this using the System class.
首先,我们将展示如何使用System类来实现这一目标。
Then, we’ll see how to use the Java Native Access (JNA) API to check the bitness of the JVM. JNA is a community-developed library that enables all native access.
然后,我们将看到如何使用Java Native Access(JNA)API来检查JVM的比特性。JNA是一个社区开发的库,可以实现所有的本地访问。
2. Using the sun.arch.data.model System Property
2.使用sun.arch.data.model系统属性
The System class in Java provides access to externally defined properties and environment variables. It maintains a Properties object that describes the configuration of the current working environment.
Java中的System类提供对外部定义的属性和环境变量的访问。它维护着一个Properties对象,描述了当前工作环境的配置。
We can use the “sun.arch.data.model” system property to identify JVM bitness:
我们可以使用”sun.arch.data.model“系统属性来识别JVM的比特性。
System.getProperty("sun.arch.data.model");
It contains “32” or “64” to indicate a 32-bit or 64-bit JVM, respectively. Although this approach is easy to use, It returns “unknown” if the property is not present. Hence, it will work only with Oracle Java versions.
它包含 “32 “或 “64”,分别表示一个32位或64位的JVM。虽然这种方法很容易使用,但如果该属性不存在,它将返回 “未知”。因此,它只适用于Oracle Java版本。
Let’s see the code:
让我们看看这段代码。
public class JVMBitVersion {
public String getUsingSystemClass() {
return System.getProperty("sun.arch.data.model") + "-bit";
}
//... other methods
}
Let’s check this approach through a unit test:
让我们通过一个单元测试来检查这个方法。
@Test
public void whenUsingSystemClass_thenOutputIsAsExpected() {
if ("64".equals(System.getProperty("sun.arch.data.model"))) {
assertEquals("64-bit", jvmVersion.getUsingSystemClass());
} else if ("32".equals(System.getProperty("sun.arch.data.model"))) {
assertEquals("32-bit", jvmVersion.getUsingSystemClass());
}
}
3. Using the JNA API
3.使用JNA API
JNA (Java Native Access) supports various platforms such as macOS, Microsoft Windows, Solaris, GNU, and Linux.
JNA(Java Native Access)支持各种平台,如macOS、Microsoft Windows、Solaris、GNU和Linux。
It uses native functions to load a library by name and retrieve a pointer to a function within that library.
它使用本地函数按名称加载一个库,并检索指向该库中一个函数的指针。
3.1. Native Class
3.1.原生类
We can use POINTER_SIZE from the Native class. This constant specifies the size (in bytes) of a native pointer on the current platform.
我们可以使用POINTER_SIZE,来自Native类。这个常数指定了当前平台上本地指针的大小(以字节为单位)。
A value of 4 indicates a 32-bit native pointer, while a value of 8 indicates a 64-bit native pointer:
4的值表示一个32位的本地指针,而8的值表示一个64位的本地指针。
if (com.sun.jna.Native.POINTER_SIZE == 4) {
// 32-bit
} else if (com.sun.jna.Native.POINTER_SIZE == 8) {
// 64-bit
}
3.2. Platform Class
3.2.Platform类
Alternatively, we can use the Platform class, which provides simplified platform information.
另外,我们可以使用Platform类,它提供简化的平台信息。
It contains the is64Bit() method that detects whether the JVM is 64-bit or not.
它包含检测JVM是否为64位的is64Bit()方法。
Let see how it identifies the bitness:
让我们看看它是如何识别比特性的。
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;
}
Here, the ARCH constant is derived from the “os.arch” property via the System class. It is used to get operating system architecture:
这里,ARCH常数通过System类从”os.arch“属性派生出来。它被用来获取操作系统架构。
ARCH = getCanonicalArchitecture(System.getProperty("os.arch"), osType);
This approach works for different operating systems and also with different JDK vendors. Hence, it is more reliable than the “sun.arch.data.model” system property.
这种方法适用于不同的操作系统,也适用于不同的JDK供应商。因此,它比”sun.arch.data.model“系统属性更可靠。
4. Conclusion
4.总结
In this tutorial, we learned how to check the JVM bit version. We also observed how JNA simplified the solution for us on different platforms.
在本教程中,我们学习了如何检查JVM的位版本。我们还观察了JNA是如何在不同平台上为我们简化解决方案的。
As always, the complete code is available over on GitHub.
一如既往,完整的代码可在GitHub上获得,。