下面是在 Window10 系统平台通过 Java 代码获取 CPU 序列号的示例:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Demo demo = new Demo();
String[] serials = demo.getCPUSerialNo();
for(String serial : serials) {
System.out.println(serial);
}
}
/**
* 获取CPU序列号
* wmic cpu get ProcessorId
* @return
*/
private String[] getCPUSerialNo() {
List<String> codeList = new ArrayList<>();
Scanner sc = null;
try {
Process process = Runtime.getRuntime().exec(
new String[]{"wmic", "cpu", "get", "ProcessorId"});
process.getOutputStream().close();
sc = new Scanner(process.getInputStream());
sc.next(); // ProcessorId
while(sc.hasNext()) {
codeList.add(sc.next());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != sc) {
sc.close();
}
}
return codeList.toArray(new String[]{});
}
}运行示例,输出如下:
BFEBF******40651