简体   繁体   English

Java NetworkInterface getName() 在 Windows 上损坏了?

[英]Java NetworkInterface getName() broken on windows?

I am writing a program for windows which lists all network adapters and deactivates all except the one selected.我正在为 Windows 编写一个程序,它列出了所有网络适配器并停用了除选定的之外的所有网络适配器。 I know how to modify network adapters with netsh, but the problem is I can't get the relevant information from the NetworkInterface class.我知道如何使用 netsh 修改网络适配器,但问题是我无法从 NetworkInterface 类中获取相关信息。

The NetworkInterface class has the methods getName() and getDisplayName(), but they seem to produce only irrelevant data. NetworkInterface 类具有 getName() 和 getDisplayName() 方法,但它们似乎只生成不相关的数据。

I took the example directly from oracle:我直接从 oracle 拿了这个例子:

http://docs.oracle.com/javase/tutorial/networking/nifs/listing.html 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 .但我无法使用此信息,因为 netsh 需要适配器的名称,即Ethernet 1而不是Intel(R) Ethernet Connection (2) I219-Veth4
In my opinion getName() should return a OS specific name with usage in the OS itself.我认为 getName() 应该返回一个操作系统特定的名称,并在操作系统本身中使用。 Is there any possibility to retrive the real name of the adapter with other standard java classes?是否有可能用其他标准 java 类检索适配器的真实名称?

At the moment my only solution would involve getmac /fo csv /v to get the right information and parse it.目前我唯一的解决方案是使用getmac /fo csv /v来获取正确的信息并对其进行解析。 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.由于我不想编写自己的包装器来访问 c++ 函数GetAdaptersAddresses ,因为我需要学习它。

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:所以对于任何想知道的人来说,tl;dr 是来自本机实现的注释,它枚举了 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.所以是的,它(故意)坏了,因为 Windows 95 是一个东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:NetworkInterface.getName()是唯一的标识符,该标识符不会随时间变化吗? - Java: Is NetworkInterface.getName() a unique identifier that will not change over time? Windows 7 上的哪个配置会影响 Java 中的 NetworkInterface.getHardwareAddress()? - Which configuration on Windows 7 could have an impact on NetworkInterface.getHardwareAddress() in Java? 为什么java NetworkInterface.getHardwareAddress在Windows上返回空字节数组? - Why does java NetworkInterface.getHardwareAddress return empty byte array on Windows? Java,将NetworkInterface拆分为ip个地址 - Java, split NetworkInterface by ip addresses Java getName(字符串名称) - Java getName(String name) JAVA path.getName(0) 返回整个路径而不是 Mac 上的第一个元素,windows 路径 - JAVA path.getName(0) returning the entire path instead of first element on a Mac, with windows path 激活MouseListener的obj的Java getName - Java getName of obj that activated MouseListener Java:使用getName()方法的标准接口? - java: standard interface with getName() method? 如何模拟java.net.NetworkInterface? - How to mock java.net.NetworkInterface? 如何欺骗NetworkInterface.getHardwareAddress()(Java) - How to spoof NetworkInterface.getHardwareAddress() (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM