10

是否可以在 java 中检测处理器架构?像 x86 或 sun SPARC 等?如果是这样,我将如何去做?

4

3 回答 3

8

You can try the System.getenv() to get environment variables, use the PROCESSOR_ARCHITECTURE Key to get the CPU-architechture:

System.out.println(System.getenv("PROCESSOR_ARCHITECTURE"));

or in case of 64 bit:

System.out.println(System.getenv("PROCESSOR_ARCHITEW6432"));

The other way would be to use the "os.arch" system property:

System.getProperty("os.arch");

and you may need to get the OS before using System.getProperty("os.name") since this is OS dependent as QMuhammad mentioned in his answer.

Notice that:

System properties and environment variables are both conceptually mappings between names and values. Both mechanisms can be used to pass user-defined information to a Java process.

Relevant links:

于 2013-03-06T06:56:03.593 回答
6
System.getProperty ("os.arch");

On my PC returns amd64.

于 2013-03-06T06:56:53.353 回答
4

您可以使用以下属性来获取处理器架构:

     System.getProperty("sun.cpu.isalist");

它返回“amd64”,因为我使用的是英特尔的 64 位处理器,而英特尔 64 位使用的是 amd 架构。

如果您需要操作系统架构值,您可以使用此属性“os.arch”

如果您需要任何其他财产,那么这可能会对您有所帮助。我编写了以下代码段来获取所有系统属性:

    public static void main(String[] args) {
    Properties props = System.getProperties();
    Enumeration<Object> keys = props.keys();

    while(keys.hasMoreElements()){
        Object key = keys.nextElement();
        Object value = props.get(key);
        System.out.println("Key: "+key + " Value: "+value);
    }
}
于 2013-03-06T07:15:31.273 回答