简体   繁体   中英

Method to tell if I am running inside a 32 or 64 Bit JVM

This is a common enough question and always seems to throw up the usual arguments between using sun.arch.data.model" OR "os.arch" as a way of telling.

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

I have put this quick method together, can anyone spot any loop holes as I want to cover all possibilities as our app may be deployed on AWS servers or standalone machines

private static boolean is64BitJVM()
{
    String jvmBitSizeUsingSunProperty = System.getProperty("sun.arch.data.model");
    if( jvmBitSizeUsingSunProperty != null && jvmBitSizeUsingSunProperty.length() > 0)
    {
        return jvmBitSizeUsingSunProperty.contains("64");
    }
    else
    {
        String jvmBitSizeUsing_os_Property = System.getProperty("os.arch");
        if( jvmBitSizeUsing_os_Property != null && jvmBitSizeUsing_os_Property.length() > 0)
        {
            return jvmBitSizeUsing_os_Property.contains("64");
        }
    }
    return false;
}

Instead of doing

if (test)
{
    return true;
}
else
{
    return false;
}

you can just do

return test;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM