Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java确定Windows的32位或64位体系结构?_Java_Windows_X86_64 Bit - Fatal编程技术网

如何使用Java确定Windows的32位或64位体系结构?

如何使用Java确定Windows的32位或64位体系结构?,java,windows,x86,64-bit,Java,Windows,X86,64 Bit,如何使用Java确定Windows的32位或64位体系结构?您可以使用系统属性中的os.arch属性来了解 System.getProperty("os.arch"); Properties pr = System.getProperties(); System.out.println(pr.getProperty("os.arch")); 如果您使用的是32位,它应该显示i386或其他内容请注意,os.arch属性将只提供JRE的体系结构,而不是底层操作系统的体系结构 如果在64位系统上安

如何使用Java确定Windows的32位或64位体系结构?

您可以使用系统属性中的os.arch属性来了解

System.getProperty("os.arch");
Properties pr = System.getProperties();
System.out.println(pr.getProperty("os.arch"));

如果您使用的是32位,它应该显示i386或其他内容请注意,
os.arch
属性将只提供JRE的体系结构,而不是底层操作系统的体系结构

如果在64位系统上安装32位jre,
system.getProperty(“os.arch”)
将返回
x86


为了实际确定底层架构,您需要编写一些本机代码。请参阅以获取更多信息(以及指向本机代码示例的链接)

我并不完全信任读取os.arch系统变量。而当用户在64位系统上运行64位JVM时,它就起作用了。如果用户在64位系统上运行32位JVM,则它不起作用

以下代码用于正确检测Windows 64位操作系统。在Windows 64位系统上,环境变量 将设置“程序文件(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);
}

对于其他操作系统,如Linux、Solaris或Mac,我们可能也会看到这个问题。所以这不是一个完整的解决方案。对于mac来说,你可能是安全的,因为苹果会锁定JVM以匹配操作系统。但是Linux和Solaris等等。。他们可能仍然在64位系统上使用32位JVM。所以请谨慎使用。

您可以尝试这段代码,我认为最好检测JVM的模型

boolean is64bit = System.getProperty("sun.arch.data.model").contains("64");

也许这不是最好的方法,但它是有效的

我所做的就是获取windows为x86文件夹中的程序文件配置的“环境变量”。我的意思是Windows x64有文件夹(程序文件x86),而x86没有。由于用户可以更改
环境变量中的程序文件路径
,或者他/她可以在C:\中创建目录“Program Files(x86)”,因此我不会检测文件夹,而是使用windows注册表中变量“Program Files(x86)”的“Environment path”

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);

    }

}
我使用命令提示符(命令-->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();

    }

}
(仅适用于Windows)检查是否 C:\Windows\SysWOW64 存在。 如果目录存在,则它是一个64位进程。
否则,它是一个32位的进程。

我想与大家分享我的Java代码解决方案(类似的是本机代码)

我想补充一下范伟贤议员的答覆;由于属性os.arch
System.getProperty(“os.arch”)
返回JRE的比特数,这实际上非常有用。从文章中:

在代码中,您首先需要检查IntPtr的大小,如果它返回8,那么您是在64位操作系统上运行的。如果返回4,则表示您正在运行一个32位应用程序,因此现在需要知道您是以本机方式运行还是在WOW64下运行

因此,IntPtr大小检查与通过查看“os.arch”执行的检查相同。在此之后,您可以继续确定进程是以本机方式运行还是在WOW64下运行

这可以使用jna库(例如)来完成,它提供了您所需的本机函数的使用

//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。另外,这应该是更安全的方法,因为在下面的示例中,您实际上不会检查“IsWow64Process”函数是否存在

在这里,我添加了一个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);
}
Map archMap=newhashmap();
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){
抛出新的IllegalArgumentException(“未知架构”+SystemUtils.OS_ARCH);
}

在64b windows上,但32b jvm为我打印“x86”。所以我猜它不是说系统的版本,而是说虚拟机。你为什么想知道这个?Java应该在任何有JVM的操作系统上运行相同的程序-您的Java程序不应该关心它是在32位还是64位操作系统上运行。@Jesper,因为很多时候运行Java并不是唯一的目的。很多时候,您可能已经开发了一个程序/应用程序,该程序/应用程序应该专门在64位机器(或32位机器)上运行(原因可能是任何东西)。在这种情况下,我需要检查windows体系结构的版本。相关:@Jesper区分32位或64位操作系统最重要的另一种情况是Java程序必须启动外部可执行文件,这两个版本都存在。就像编写Selenium测试一样,必须在其中启动正确的浏览器驱动程序。对于识别操作系统本身也存在同样的需求。我相信人们还能想到更多的情况,一些本应被“抽象”掉的东西是不存在的。@Jesper你说的是:用户Boolean已经有了相同的方式,但代码更好。OP询问的操作系统不是JVM,也不是JVM,在Windows 10(64位)上用Java 8 32位检查。它可以工作,也可以不工作;在某些操作系统中,此键不存在。检查,投票最多的答案。在我的Win10中它是存在的。在运行32位JVM的64位操作系统上,这将返回x86,表示32位。虽然这对于JVM来说是正确的,但对于操作系统来说是错误的,这是OP提出的问题。“我不完全相信读取
OS.arch
系统变量。”这种行为的原因是,例如,对于操作系统来说,重要的是了解其架构,而不是底层操作系统(如果您需要将自己的平台相关共享库加载到Java代码中,它们需要与JVM相同)。+1经过大量研究后,我将使用它。但我很好奇,在这台机器(Windows 8.1 64位)上,环境变量