本文介绍了Windows上的Java NetworkInterface getName()损坏了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Windows编写一个程序,其中列出了所有网络适配器,并停用了所有选定的适配器.我知道如何使用netsh修改网络适配器,但是问题是我无法从NetworkInterface类获取相关信息.

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.

NetworkInterface类具有方法getName()和getDisplayName(),但它们似乎只产生不相关的数据.

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

我直接从oracle那里举了一个例子:

I took the example directly from oracle:

http://docs.oracle.com/javase/tutorial/联网/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

但是我不能使用此信息,因为netsh需要适配器的名称,它是Ethernet 1而不是Intel(R) Ethernet Connection (2) I219-Veth4.
我认为getName()应该返回一个特定于操作系统的名称,并在操作系统本身中使用.是否有可能与其他标准Java类一起检索适配器的真实名称?

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?

目前,我唯一的解决方案是使用getmac /fo csv /v来获取正确的信息并进行解析.由于我不想编写自己的包装来访问c ++函数GetAdaptersAddresses的事实,因为我需要学习它.

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.

推荐答案

我刚刚回答了类似问题,碰到了这个.因此,对于任何想知道的人,tl; dr是此注释来自枚举JVM中接口名称的本机实现:

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.
 */

是的,由于Windows 95是一件事,它已经被故意破坏了.

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

这篇关于Windows上的Java NetworkInterface getName()损坏了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 07:29
查看更多