免责声明:请不要把它否决为一个重复的答案,因为我想和大家分享我的Java代码解决方案(一个同样是本地代码)。
我想把詹姆斯·范·赫斯先生的回答加起来;
由于os.arch属性:
System.getProperty("os.arch");
返回JRE的位,这实际上非常有用。
从文章中:
在代码中,首先需要检查intptr的大小,如果它返回8,那么您将在64位操作系统上运行。如果它返回4,那么您正在运行一个32位的应用程序,所以现在您需要知道您是在本机运行还是在wow64下运行。
因此,intptr大小检查与通过查看“os.arch”执行的检查相同。在此之后,您可以继续确定进程是本机运行还是在WOW64下运行。
可以使用JNA库(例如
NativeLibrary
)它提供了您所需要的本机函数的使用。
//test the JRE here by checking the os.arch property
//go into the try block if JRE is 32bit
try {
NativeLibrary kernel32Library = NativeLibrary.getInstance("kernel32");
Function isWow64Function = kernel32Library.getFunction("IsWow64Process");
WinNT.HANDLE hProcess = Kernel32.INSTANCE.GetCurrentProcess();
IntByReference isWow64 = new IntByReference(0);
Boolean returnType = false;
Object[] inArgs = {
hProcess,
isWow64
};
if ((Boolean) isWow64Function.invoke(returnType.getClass(), inArgs)) {
if (isWow64.getValue() == 1) {
//32bit JRE on x64OS
}
}
} catch (UnsatisfiedLinkError e) { //thrown by getFunction
}
类似的东西也可以,但是我建议使用第一个版本,因为它是我在x64上测试的版本,而在x64操作系统上是32位JRE。另外,这应该是更安全的方法,因为在下面的内容中,您实际上并不检查“isww64进程”函数是否存在。
在这里,我添加了一个JRE检查的例子,尽管并不难找到,但它是完整的。
Map archMap = new HashMap();
archMap.put("x86", 32);
archMap.put("i386", 32);
archMap.put("i486", 32);
archMap.put("i586", 32);
archMap.put("i686", 32);
archMap.put("x86_64", 64);
archMap.put("amd64", 64);
//archMap.put("powerpc", 3);
this.arch = archMap.get(SystemUtils.OS_ARCH);
if (this.arch == null) {
throw new IllegalArgumentException("Unknown architecture " + SystemUtils.OS_ARCH);
}