开发者

How to test from java code if the jvm supports -d64 option?

I thought about using the following c开发者_Python百科ode, 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());


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜