2

After some research on the same topic, I used the method "getVersion()" to check whether a JVM is 64 bit or 32 bit.

 public static String getVersion()
 {
   String version = System.getProperty("sun.arch.data.model");
   if (version != null && version.contains("64")){
       return "64";
   }else{
       return "32";
   }
 }

It went wrong in some cases. Yes as the flag name mentions, the method is clearly sun-dependent. I tried getting the property "os.arch" also. But in some cases, it is wrongly identifying JVM. Is there any more trustable way of checking the same? My application is purely based on windows. And I don't want the method to work on any other platforms.

Nidhin Joseph
  • 1,461
  • 3
  • 15
  • 32
  • possible duplicate of [How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?](http://stackoverflow.com/questions/2062020/how-can-i-tell-if-im-running-in-64-bit-jvm-or-32-bit-jvm-from-within-a-program) – Joe Jun 14 '14 at 06:23
  • @Nidhin Do you mind using JNI and an extra DLL file? – Mohammad Banisaeid Jun 14 '14 at 06:33
  • 1
    "And I don't want the method to work on any other platforms." This is somewhat contradictory to one of the main philosophies of Java -- write once, run anywhere. Best you can do probably is a `native` call. – awksp Jun 14 '14 at 06:41
  • @Joe No bro. I don't think so. Please go through my question again. I saw that link. And even that answer is not completely solving my problem. I think the question is very clear. – Nidhin Joseph Jun 14 '14 at 18:01
  • @user3580294. even the swt library is having different bundles for different platforms. Then why my app have to make use of every advantage of the powerful language Java. ;) – Nidhin Joseph Jun 14 '14 at 18:04
  • Regardless of how SWT is implemented, the *Java* parts of it are write-once-run-anywhere, just like all the other parts of Java. Just like the JDK and JRE themselves -- different implementations for different platforms, but the API exposed to the programmer is the same (or as close to the same as possible) – awksp Jun 14 '14 at 19:11
  • 2
    You say "It went wrong in some cases." Any more detail? The linked question is an Oracle-recommended method for exactly this. – Joe Jun 15 '14 at 10:32
  • I support @Joe's comment. `os.arch` is supposed to work correctly. If it does not - there is a bug that should be reported to Oracle. – apangin Jun 15 '14 at 10:53
  • @apangin It might not be a bug. But I think it is always returning the property of JVM created by my default JDK. But when i am trying to run my application using a non-default jdk path, the code fails to get the property of current running JVM. Anyways the answer provided by you solved my issue. Thank you so much. – Nidhin Joseph Jun 15 '14 at 12:28

2 Answers2

2

Here is a pure Java solution that checks the ability to link 32-bit library into the current process:

static boolean is64bitProcess() {
    String wow64_kernel = System.getenv("systemroot") + "\\SysWOW64\\kernel32.dll";
    if (new File(wow64_kernel).exists()) {
        try {
            System.load(wow64_kernel);
        } catch (UnsatisfiedLinkError e) {
            return true; // can not link 32-bit library into 64-bit process
        }
    }
    return false;
}
apangin
  • 92,924
  • 10
  • 193
  • 247
1

You will need the 2 jar files from here : https://java.net/projects/jna/downloads/directory/3.3.0

(Code edited to fix evaluation of IsWow64Process)

import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;

public class Main {

    public static void main(String[] args) {
        System.out.println(is64BitJava());
    }

    private static boolean is64BitJava(){
        if (!is64BitWindows()){
            return false;
        }

        Kernel32 kernel32 = Kernel32.INSTANCE;
        WinNT.HANDLE handle = kernel32.GetCurrentProcess();
        if (!kernel32.IsWow64Process(handle, ref)){
            return false;
        }

        return ref.getValue() == 0;

    }

    private static boolean is64BitWindows(){
        String envVar = System.getenv("ProgramW6432");
        if (envVar == null){
            envVar = "";
        }

        return envVar.length() > 0;
    }

}

In order to check if Windows is 64 bit, I check if ProgramW6432 environment variable is defined.

Then I use Win32 API GetCurrentProcess and IsWow64Process functions to examine current running process.

Mohammad Banisaeid
  • 2,376
  • 27
  • 35
  • I will give this a try. Thanks for your valuable answer. – Nidhin Joseph Jun 14 '14 at 18:10
  • 1
    This might not help. JNA library internally contains DLLs for most popular platforms and it loads one of them in run-time. Guess how JNA detects which platform it is being run on? Right, it is `System.getProperty("os.arch")` again (see [Native.loadNativeLibraryFromJar](https://java.net/projects/jna/sources/svn/content/trunk/jnalib/src/com/sun/jna/Native.java)). OP claims that `os.arch` may work wrong, that means that JNA library will fail to load. – apangin Jun 15 '14 at 10:47