简体   繁体   中英

How to get cpu type in java?

I want to retrieve a procesor tipe in java program(like "Ivy bridge").I look for some Sytem comand like:

System.out.println(System.getenv("PROCESSOR_IDENTIFIER"));
System.out.println(System.getenv("PROCESSOR_ARCHITECTURE"));
System.out.println(System.getenv("PROCESSOR_ARCHITEW6432"));
System.out.println(System.getenv("NUMBER_OF_PROCESSORS"));

but i don't find what i want.

You can query the registry. This can be done easily with JNA. Works for Windows only.

import com.sun.jna.platform.win32.Advapi32Util;
import static com.sun.jna.platform.win32.WinReg.HKEY_LOCAL_MACHINE;

    public class GetCPUInfosUsingJNA {

        // https://github.com/twall/jna#readme
        //  you need 2 jars : jna-3.5.1.jar and platform-3.5.1.jar

        public static void main(String ... args) {
          System.out.println(Advapi32Util.registryGetStringValue
             (HKEY_LOCAL_MACHINE,
                "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\",
              "ProcessorNameString"));   
          System.out.println(Advapi32Util.registryGetStringValue
                  (HKEY_LOCAL_MACHINE,
                     "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\",
                   "Identifier"));        
        }
    }

如果你正在使用windows使用jawin来访问win32 api和linux发行版读取/ proc / cpuinfo并解析它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM