如何使用Java 5获取主机mac地址?

Dav*_*ave 4 java java-5

我知道你可以使用Java 6来做到这一点java.net.NetworkInterface->getHardwareAddress().但是我部署的环境仅限于Java 5.

有人知道如何在Java 5或更早版本中执行此操作吗?非常感谢.

but*_*ken 6

Java 5中的标准方法是启动本机进程来运行ipconfigifconfig解析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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)