我知道您可以使用Java 6来做到这一点java.net.NetworkInterface->getHardwareAddress()。但是我要部署的环境仅限于Java 5。
java.net.NetworkInterface->getHardwareAddress()
有谁知道如何在Java 5或更早版本中执行此操作?非常感谢。
Java 5中的标准方法是启动一个本机进程来运行ipconfig或ifconfig解析OutputStream以获得您的答案。
例如:
private String getMacAddress() throws IOException { String command = “ipconfig /all”; Process pid = Runtime.getRuntime().exec(command); BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream())); Pattern p = Pattern.compile(”.*Physical Address.*: (.*)”); while (true) { String line = in.readLine(); if (line == null) break; Matcher m = p.matcher(line); if (m.matches()) { return m.group(1); } } }