Java NetworkInterface getName() broken on windows?

给你一囗甜甜゛ 提交于 2019-12-12 01:46:05

问题


I am writing a program for windows which lists all network adapters and deactivates all except the one selected. I know how to modify network adapters with netsh, but the problem is I can't get the relevant information from the NetworkInterface class.

The NetworkInterface class has the methods getName() and getDisplayName(), but they seem to produce only irrelevant data.

I took the example directly from oracle:

http://docs.oracle.com/javase/tutorial/networking/nifs/listing.html

public class ListNets {

  public static void main(String args[]) throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets))
      displayInterfaceInformation(netint);
  }

  static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    out.printf("Display name: %s\n", netint.getDisplayName());
    out.printf("Name: %s\n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
      out.printf("InetAddress: %s\n", inetAddress);
    }
    out.printf("\n");
  }
}  

This example produces this kind of output:

Display name: Intel(R) Ethernet Connection (2) I219-V
Name: eth4
InetAddress: /10.0.0.4
InetAddress: /fe80:0:0:0:cc75:ed6:3089:bd61%eth4

But I can't use this information, because netsh requires the name of the adapter, which is Ethernet 1 not Intel(R) Ethernet Connection (2) I219-V or eth4.
In my opinion getName() should return a OS specific name with usage in the OS itself. Is there any possibility to retrive the real name of the adapter with other standard java classes?

At the moment my only solution would involve getmac /fo csv /v to get the right information and parse it. Due to the fact that i don't want to write my own wrapper which accesses the c++ function GetAdaptersAddresses, because i would need to learn it.


回答1:


I've just answered a similar question and ran into this one. So to anyone wondering, the tl;dr is this comment from the native implementation that enumerates interface names in the JVM:

/*
 * Windows implementation of the java.net.NetworkInterface native methods.
 * This module provides the implementations of getAll, getByName, getByIndex,
 * and getByAddress.
 *
 * Interfaces and addresses are enumerated using the IP helper routines
 * GetIfTable, GetIfAddrTable resp. These routines are available on Windows
 * 98, NT SP+4, 2000, and XP. They are also available on Windows 95 if
 * IE is upgraded to 5.x.
 *
 * Windows does not have any standard for device names so we are forced
 * to use our own convention which is based on the normal Unix naming
 * convention ("lo" for the loopback, eth0, eth1, .. for ethernet devices,
 * tr0, tr1, .. for token ring, and so on). This convention gives us
 * consistency across multiple Windows editions and also consistency with
 * Solaris/Linux device names. Note that we always enumerate in index
 * order and this ensures consistent device number across invocations.
 */

So yeah, it's broken (on purpose) since Windows 95 was a thing.



来源:https://stackoverflow.com/questions/38803545/java-networkinterface-getname-broken-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!