下面将演示在 Windows10 系统下,怎样通过 Java 代码获取主板序列号。示例代码如下:
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.getMainboardCode(); for(String serial : serials) { System.out.println(serial); } } /** * 获取主板序列号 * @return */ public String[] getMainboardCode() { List<String> codeList = new ArrayList<>(); Scanner sc = null; try { Process process = Runtime.getRuntime().exec(new String[] { "wmic", "baseboard", "get", "serialnumber" }); process.getOutputStream().close(); sc = new Scanner(process.getInputStream()); sc.next(); // SerialNumber if(sc.hasNext()) { codeList.add(sc.next()); } } catch (Exception e) { e.printStackTrace(); } finally { if(null != sc) { sc.close(); } } return codeList.toArray(new String[]{}); } }
运行示例,输出如下:
L1HF527****