How do you determine 32 or 64 bit architecture of Windows using Java?

后端 未结 9 2106
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 03:02

How do you determine 32 or 64 bit architecture of Windows using Java?

相关标签:
9条回答
  • 2020-11-30 03:40

    Please note, the os.arch property will only give you the architecture of the JRE, not of the underlying os.

    If you install a 32 bit jre on a 64 bit system, System.getProperty("os.arch") will return x86

    In order to actually determine the underlying architecture, you will need to write some native code. See this post for more info (and a link to sample native code)

    0 讨论(0)
  • 2020-11-30 03:42

    You can try this code, I thinks it's better to detect the model of JVM

    boolean is64bit = System.getProperty("sun.arch.data.model").contains("64");
    
    0 讨论(0)
  • 2020-11-30 03:43

    I wanted to share my Java code solution to this (the one alike is a native code).

    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:

    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.

    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.

    This can be done using the jna library(e.g. NativeLibrary) which offers use of the native functions you need.

    //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.

    Here I am adding an example of the JRE check, just so it is complete, even though it's not hard to find.

    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);
    }
    
    0 讨论(0)
提交回复
热议问题