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

前端 未结 12 1794
滥情空心
滥情空心 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 09:58

    Just type java -version in your console.

    If a 64 bit version is running, you'll get a message like:

    java version "1.6.0_18"
    Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
    Java HotSpot(TM) 64-Bit Server VM (build 16.0-b13, mixed mode)
    

    A 32 bit version will show something similar to:

    java version "1.6.0_41"
    Java(TM) SE Runtime Environment (build 1.6.0_41-b02)
    Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode, sharing)
    

    Note Client instead of 64-Bit Server in the third line. The Client/Server part is irrelevant, it's the absence of the 64-Bit that matters.

    If multiple Java versions are installed on your system, navigate to the /bin folder of the Java version you want to check, and type java -version there.

    0 讨论(0)
  • 2020-11-22 09:59

    If you're using JNA, you can do thisPlatform.is64Bit().

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 10:04

    If you are using JNA, you can check whether com.sun.jna.Native.POINTER_SIZE == 4 (32 bit) or com.sun.jna.Native.POINTER_SIZE == 8 (64 bit).

    0 讨论(0)
  • 2020-11-22 10:10

    For certain versions of Java, you can check the bitness of the JVM from the command line with the flags -d32 and -d64.

    $ java -help
    ...
        -d32          use a 32-bit data model if available
        -d64          use a 64-bit data model if available
    

    To check for a 64-bit JVM, run:

    $ java -d64 -version
    

    If it's not a 64-bit JVM, you'll get this:

    Error: This Java instance does not support a 64-bit JVM.
    Please install the desired version.
    

    Similarly, to check for a 32-bit JVM, run:

    $ java -d32 -version
    

    If it's not a 32-bit JVM, you'll get this:

    Error: This Java instance does not support a 32-bit JVM.
    Please install the desired version.
    

    These flags were added in Java 7, deprecated in Java 9, removed in Java 10, and no longer available on modern versions of Java.

    0 讨论(0)
  • 2020-11-22 10:11

    Complementary info:

    On a running process you may use (at least with some recent Sun JDK5/6 versions):

    $ /opt/java1.5/bin/jinfo -sysprops 14680 | grep sun.arch.data.model
    Attaching to process ID 14680, please wait...
    Debugger attached successfully.
    Server compiler detected.
    JVM version is 1.5.0_16-b02
    sun.arch.data.model = 32
    

    where 14680 is PID of jvm running the application. "os.arch" works too.

    Also other scenarios are supported:

    jinfo [ option ] pid
    jinfo [ option ] executable core
    jinfo [ option ] [server-id@]remote-hostname-or-IP 
    

    However consider also this note:

    "NOTE - This utility is unsupported and may or may not be available in future versions of the JDK. In Windows Systems where dbgent.dll is not present, 'Debugging Tools for Windows' needs to be installed to have these tools working. Also the PATH environment variable should contain the location of jvm.dll used by the target process or the location from which the Crash Dump file was produced."

    0 讨论(0)
提交回复
热议问题