如何使用Java 5获取主机mac地址? [英] How do I get the hosts mac address using Java 5?
本文介绍了如何使用Java 5获取主机mac地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道您可以使用 java.net.NetworkInterface-> getHardwareAddress()
在Java 6中执行此操作。但是我部署的环境仅限于Java 5.
I know you can do this with Java 6 using java.net.NetworkInterface->getHardwareAddress()
. But the environment I am deploying on is restricted to Java 5.
有人知道如何在Java 5或更早版本中执行此操作吗?非常感谢。
Does anyone know how to do this in Java 5 or earlier? Many thanks.
推荐答案
Java 5中的标准方法是启动本机进程来运行 ipconfig
或 ifconfig
并解析 OutputStream
以获得答案。
The standard way in Java 5 was to start a native process to run ipconfig
or ifconfig
and parse the OutputStream
to get your answer.
例如:
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);
}
}
}
这篇关于如何使用Java 5获取主机mac地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文