How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?

前端 未结 12 1780
滥情空心
滥情空心 2020-11-22 09:39

How can I tell if the JVM in which my application runs is 32 bit or 64-bit? Specifically, what functions or properties I can used to detect this within the program?

12条回答
  •  长情又很酷
    2020-11-22 10:00

    For Windows, you can check the Java home location. If it contains (x86) it is 32-bit otherwise 64-bit:

    public static boolean is32Bit()
    {
        val javaHome = System.getProperty("java.home");
        return javaHome.contains("(x86)");
    }
    
    public static boolean is64Bit()
    {
        return !is32Bit();
    }
    

    Example paths:

    C:\Program Files (x86)\Java\jdk1.8.0_181\bin\java.exe # 32-bit
    C:\Program Files\Java\jdk-10.0.2\bin\java.exe # 64-bit
    

    Why care about a Windows only solution?

    If you need to know which bit version you're running on, you're likely fiddling around with native code on Windows so platform-independence is out of the window anyway.

提交回复
热议问题