tags:

views:

61

answers:

4

I thought about using the following code, but is there any cleaner way?

Process theProcess = Runtime.getRuntime().exec("java -d64 -version");
BufferedReader errStream = new BufferedReader(new InputStreamReader(theProcess.getErrorStream()));
System.out.println(errStream.readLine());
+1  A: 

If you are using Sun JVM, I think you can use the sun.arch.data.model system property (using the System.getproperty() to get this value.

Gangadhar
What if jvm is from a different vendor?
rkatiyar
I am not sure if the other JVMs have a property for this.
Gangadhar
+1  A: 

You can use

String arch = System.getProperty("os.arch")
if (arch.equals("amd64") || arch.equals("x86_64")) {
    // Machine is 64-bit
}

To determine whether the machine itself is a 64-bit machine. Not sure about checking the VM itself, though.

haldean
This is inaccurate and not to be trusted.
Jonathon
and what if `arch.matches("[Aa]lpha.*")` which is a a 64-bit machine? and if "x86_128" supports the -d64 option you'd say it doesn't.
Stephen P
+1  A: 

Decided to post this as an answer:

public static boolean supports64Bit() {
    try {
        final Process process = Runtime.getRuntime().exec("java -d64 -version");
        try {
            return process.waitFor() == 0;
        } finally {
            process.getInputStream().close();
            process.getOutputStream().close();
            process.getErrorStream().close();
        }
    } catch (IOException e) {
        // log error here?!
        return false;
    }
}

Closing all streams associated with a process is good practice and prevents resource leaks.

Not tested.

Willi
Another good idea is to do this only once and than cache the result in a static variable.
Willi
tested this, it works. Thanks.
rkatiyar
I'm glad to help.
Willi
A: 

The simplest way to do so, may be through Java WebStart and a local JNLP file. You then invoke "javaws foo.jnlp" with appropriate options.

You can have architecture dependent options and libraries.

Thorbjørn Ravn Andersen