Is it possible to detect processor architecture in java? [duplicate]

自作多情 提交于 2019-12-01 15:08:01

问题


Is it possible to detect processor architecture in java? like x86 or sun SPARC, etc? If so, how would I go about doing it?


回答1:


System.getProperty ("os.arch");

On my PC returns amd64.




回答2:


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:

  • System.getenv() doc
  • ChrisH's answer
  • Why %processor_architecture% always returns x86 instead of AMD64
  • Java's "os.arch" System Property is the Bitness of the JRE, NOT the Operating System
  • Finding out sytem architecture using Java



回答3:


You can use following property to get processor architecture:

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

It returns "amd64" as i am using Intel's 64 bit processor and Intel 64 bit uses amd architecture.

If you need OS architecture value you can use this property "os.arch"

And if you need any other property then this might help you. I wrote following snippet to get all system properties:

    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);
    }
}


来源:https://stackoverflow.com/questions/15240835/is-it-possible-to-detect-processor-architecture-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!