- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过 ip 地址打印连接到我的网络的其他计算机的物理地址。
使用 arp -a
我可以看到所有网络及其物理地址。
但是当运行 java 代码时,我无法检索它对应的 ip 的物理地址,它在调用 getHardwareAddress()
方法的行上给了我一个 NullPointerException
.但是如果它是我电脑的 ip,它会提供正确的输出并且不会抛出异常。
这是我得到的。
public void printMacAddress() throws UnknownHostException, SocketException {
String ipAddress = "192.168.0.105";
byte[] mac = null;
InetAddress add = InetAddress.getByName(ipAddress);
NetworkInterface network = NetworkInterface.getByInetAddress(add);
mac = network.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i],
(i < mac.length - 1) ? "-" : ""));
}
System.out.print("The mac address is : ");
System.out.println(sb.toString());
}
最佳答案
您只能为自己的网络接口(interface)获取 NetworkInterface
对象,不能为其他主机的网络接口(interface)获取对象。查看Javadoc :
It is used to identify the local interface
当您调用 NetworkInterface.getByInetAddress(add)
时,其中 add
是一个外部 IP 地址,因此它返回了您没有返回的 null,
t 检查,所以当你使用它时你得到了一个 NullPointerException
。
你不能这样做。
关于java - 方法 getHardwareAddress() 在使用其他网络的 ip 时给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21869054/
我想在 Eclipse 中调用 NetworkInterface 实例的 getHardwareAddress() 方法,但它显示“该方法 getHardwareAddress() 对于 Networ
我已经使用这个 API 来选择设备的 mac 地址, NetworkInterface.getHardwareAddress() 但这是针对 API 级别 9 及更高版本的,我应该使用什么来选择 AP
我有一个在 OpenShift (Kubernetes) 中运行的软件,其中许可证基于 MAC 地址。重新启动应用程序时,容器的MAC地址发生变化,我必须申请新的许可证文件。 由于 k8s pod 中
我正在尝试通过 ip 地址打印连接到我的网络的其他计算机的物理地址。 使用 arp -a 我可以看到所有网络及其物理地址。 但是当运行 java 代码时,我无法检索它对应的 ip 的物理地址,它在调用
我实际上是在尝试在 IBMi 上运行 OrientDB(应用了 iSeries、AS/400、V540、JDK6-32 位最后的 CUM PTF)。 OrientDB 早期所做的事情之一(可以这么说)
我在 Windows 计算机上有以下代码: for(Enumeration enm = NetworkInterface.getNetworkInterfaces(); enm.hasMoreElem
我有以下代码来提取我的 Ubuntu 设备的 MAC 地址,并且我将 MAC 地址放入数据库中。此代码在 Windows PC 中运行良好,但在 Ubuntu 设备中则不起作用。数据库字段变空。我的应
我在获取机器的 MAC 地址时遇到问题,已在 this question 中解决使用以下代码: Process p = Runtime.getRuntime().exec("getmac /fo cs
我是一名优秀的程序员,十分优秀!