您必须从操作系统获取此信息。在 Windows 上,有一个环境变量 –PROCESSOR_IDENTIFIER
– 您可以通过方法获得getenv https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/System.html#getenv(java.lang.String), as in:
System.getenv("PROCESSOR_IDENTIFIER");
在我的 Windows 10 计算机上,我得到:
Intel64 Family 6 Model 158 Stepping 11, GenuineIntel
我没有 Mac,但根据this https://github.molgen.mpg.de/pages/bs/macOSnotes/mac/mac_procs_cpu.html你可以通过类调用命令流程构建器 https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ProcessBuilder.html.
ProcessBuilder pb = new ProcessBuilder("sysctl", "-n", "machdep.cpu.brand_string");
try {
Process p = pb.start();
BufferedReader br = p.inputReader();
String output = br.readLine();
int status = p.waitFor();
if (status == 0) {
// Command succeeded.
}
}
catch (InterruptedException | IOException x) {
x.printStackTrace();
}
因此,您可能需要类似于以下内容的代码:
String details;
if ("Windows 10".equals(System.getProperty("os.name"))) {
details = System.getenv("PROCESSOR_IDENTIFIER");
}
else if ("Mac OS X".equals(System.getProperty("os.name"))) {
ProcessBuilder pb = new ProcessBuilder("sysctl", "-n", "machdep.cpu.brand_string");
try {
Process p = pb.start();
BufferedReader br = p.inputReader();
details = br.readLine();
int status = p.waitFor();
if (status == 0) {
// Command succeeded.
}
}
catch (InterruptedException | IOException x) {
x.printStackTrace();
}
}