您如何使用 Java 确定 Windows 的 32 位或 64 位体系结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1856565/
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 do you determine 32 or 64 bit architecture of Windows using Java?
提问by Matthew
How do you determine 32 or 64 bit architecture of Windows using Java?
您如何使用 Java 确定 Windows 的 32 位或 64 位体系结构?
采纳答案by James Van Huis
Please note, the os.arch
property will only give you the architecture of the JRE, not of the underlying os.
请注意,该os.arch
属性只会为您提供JRE的架构,而不是底层操作系统的架构。
If you install a 32 bit jre on a 64 bit system, System.getProperty("os.arch")
will return x86
如果在 64 位系统上安装 32 位 jre,System.getProperty("os.arch")
将返回x86
In order to actually determine the underlying architecture, you will need to write some native code. See this postfor more info (and a link to sample native code)
为了实际确定底层架构,您需要编写一些本机代码。有关更多信息,请参阅此帖子(以及指向示例本机代码的链接)
回答by Bozho
System.getProperty("os.arch");
回答by prasanna
You can use the os.arch property in system properties to find out.
您可以使用系统属性中的 os.arch 属性来查找。
Properties pr = System.getProperties();
System.out.println(pr.getProperty("os.arch"));
If you are on 32 bit, it should show i386 or something
如果您使用的是 32 位,它应该显示 i386 或其他内容
回答by Boolean
I don't exactly trust reading the os.arch system variable. While it works if a user is running a 64bit JVM on a 64bit system. It doesn't work if the user is running a 32bit JVM on a 64 bit system.
我并不完全相信阅读 os.arch 系统变量。如果用户在 64 位系统上运行 64 位 JVM,它可以工作。如果用户在 64 位系统上运行 32 位 JVM,则它不起作用。
The following code works for properly detecting Windows 64-bit operating systems. On a Windows 64 bit system the environment variable "Programfiles(x86)" will be set. It will NOT be set on a 32-bit system and java will read it as null.
以下代码适用于正确检测 Windows 64 位操作系统。在 Windows 64 位系统上,将设置环境变量“Programfiles(x86)”。它不会在 32 位系统上设置,java 会将其读取为 null。
boolean is64bit = false;
if (System.getProperty("os.name").contains("Windows")) {
is64bit = (System.getenv("ProgramFiles(x86)") != null);
} else {
is64bit = (System.getProperty("os.arch").indexOf("64") != -1);
}
For other operating systems like Linux or Solaris or Mac we may see this problem as well. So this isn't a complete solution. For mac you are probably safe because apple locks down the JVM to match the OS. But Linux and Solaris, etc.. they may still use a 32-bit JVM on their 64-bit system. So use this with caution.
对于 Linux 或 Solaris 或 Mac 等其他操作系统,我们也可能会遇到此问题。所以这不是一个完整的解决方案。对于 mac 你可能是安全的,因为苹果锁定了 JVM 以匹配操作系统。但是 Linux 和 Solaris 等。他们可能仍然在他们的 64 位系统上使用 32 位 JVM。所以请谨慎使用。
回答by user2181778
You can try this code, I thinks it's better to detect the model of JVM
你可以试试这段代码,我觉得检测JVM的模型比较好
boolean is64bit = System.getProperty("sun.arch.data.model").contains("64");
回答by Blackgeo32
Maybe it 's not the best way, but it works.
也许这不是最好的方法,但它有效。
All I do is get the "Enviroment Variable" which windows has configured for Program Files x86 folder. I mean Windows x64 have the folder (Program Files x86) and the x86 does not. Because a user can change the Program Files path in Enviroment Variables
, or he/she may make a directory "Program Files (x86)" in C:\, I will not use the detection of the folder but the "Enviroment Path" of "Program Files (x86)" with the variable in windows registry.
我所做的就是获取 Windows 为 Program Files x86 文件夹配置的“环境变量”。我的意思是 Windows x64 有文件夹 (Program Files x86) 而 x86 没有。由于用户可以更改程序文件路径Enviroment Variables
,或者他/她可以做一个目录“程序文件(x86)”,在C:\,我不会使用检测的文件夹,但的“程序文件“环境路径” (x86)" 与 Windows 注册表中的变量。
public class getSystemInfo {
static void suckOsInfo(){
// Get OS Name (Like: Windows 7, Windows 8, Windows XP, etc.)
String osVersion = System.getProperty("os.name");
System.out.print(osVersion);
String pFilesX86 = System.getenv("ProgramFiles(X86)");
if (pFilesX86 !=(null)){
// Put here the code to execute when Windows x64 are Detected
System.out.println(" 64bit");
}
else{
// Put here the code to execute when Windows x32 are Detected
System.out.println(" 32bit");
}
System.out.println("Now getSystemInfo class will EXIT");
System.exit(0);
}
}
回答by Crusaderpyro
I used the command prompt (command --> wmic OS get OSArchitecture) to get the OS architecture. The following program helps get all the required parameters:
我使用命令提示符(命令 --> wmic OS get OSArchitecture)来获取操作系统架构。以下程序有助于获取所有必需的参数:
import java.io.*;
public class User {
public static void main(String[] args) throws Exception {
System.out.println("OS --> "+System.getProperty("os.name")); //OS Name such as Windows/Linux
System.out.println("JRE Architecture --> "+System.getProperty("sun.arch.data.model")+" bit."); // JRE architecture i.e 64 bit or 32 bit JRE
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c","wmic OS get OSArchitecture");
builder.redirectErrorStream(true);
Process p = builder.start();
String result = getStringFromInputStream(p.getInputStream());
if(result.contains("64"))
System.out.println("OS Architecture --> is 64 bit"); //The OS Architecture
else
System.out.println("OS Architecture --> is 32 bit");
}
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}
回答by Olligaming1107
(Only for Windows) Check if C:\Windows\SysWOW64 exists. if the directory exist, it is a 64 bit process. Else, it is a 32 bit process.
(仅适用于 Windows)检查 C:\Windows\SysWOW64 是否存在。如果目录存在,则它是一个 64 位进程。否则,它是一个 32 位进程。
回答by wckan_
I wanted to share my Java code solution to this (the one alike is a native code).
我想分享我的 Java 代码解决方案(类似的是本机代码)。
I would like to add up to Mr James Van Huis's answer; since the property os.arch System.getProperty("os.arch")
returns the bitness of JRE, this can actually be very useful. From the article:
我想加上 James Van Huis 先生的回答;由于属性 os.archSystem.getProperty("os.arch")
返回 JRE 的位数,这实际上非常有用。从文章:
In your code, you first need to check the size of IntPtr, if it returns 8 then you are running on a 64-bit OS. If it returns 4, you are running a 32 bit application, so now you need to know whether you are running natively or under WOW64.
在您的代码中,您首先需要检查 IntPtr 的大小,如果它返回 8,那么您在 64 位操作系统上运行。如果返回 4,则您正在运行 32 位应用程序,因此现在您需要知道您是在本机运行还是在 WOW64 下运行。
Therefore, the IntPtr size check is the same check you perform by looking at the "os.arch". After this you can proceed with figuring out whether the process is running natively or under WOW64.
因此,IntPtr 大小检查与您通过查看“os.arch”执行的检查相同。在此之后,您可以继续确定该进程是在本机运行还是在 WOW64 下运行。
This can be done using the jna library(e.g. NativeLibrary) which offers use of the native functions you need.
这可以使用 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
}
Something like this might also work, but I would recommend the first version, since it's the one I tested on x64 and 32bit JRE on x64 OS. Also it should be the safer way, because in the following you don't actually check whether or not the "IsWow64Process" function exists.
像这样的东西也可能有效,但我会推荐第一个版本,因为它是我在 x64 操作系统上的 x64 和 32 位 JRE 上测试过的版本。它也应该是更安全的方法,因为在下面你实际上并不检查“IsWow64Process”函数是否存在。
Here I am adding an example of the JRE check, just so it is complete, even though it's not hard to find.
这里我添加了一个 JRE 检查的例子,只是为了它是完整的,即使它不难找到。
Map<String, Integer> archMap = new HashMap<String, Integer>();
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);
}