1

Simple piece of testing code:

public class Runner {
    public static void main(String[] args) throws SocketException {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

        while (networkInterfaces.hasMoreElements()) {
                System.out.println(networkInterfaces.nextElement().getName());
        }
    }
}

And the output:

docker0
enp3s0
lo

Output of ifconfig:

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    <truncated>

enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    <truncated>

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    <truncated>

wlp0s20u7: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    <truncated>

From my code, the interface wlp0s20u7 does not show up. This does happen though when I am not connected to any wireless network though. On connection, it does show up for wlp0s20u7. Is there a way to get the NetworkInterface object without a wireless connection?

Rohan Prabhu
  • 7,180
  • 5
  • 37
  • 71
  • Does your wlp0s20u7 have an IP address (when it's "not connected to any wireless")? – jbm Apr 09 '16 at 15:06
  • No it doesn't. Is that the issue? – Rohan Prabhu Apr 09 '16 at 18:25
  • Most probably (I'd say 95%). I don't now much about Java, in my understanding it is just too high-level (by design) to make any use of an interface without an IP address. Examples that I would think of: are there Java standard API to get/set the MAC address of an interface, change it's oper state (UP / down), get its link layer type (802.1 aka Ethernet vs 802.11 PHY aka Wi-Fi), get/set its MTU etc. This is closer to the OS/hardware, which Java is precisely and cleverly design to abstract. Well, it is a _virtual_ machine, and it does not give access to the _actual_ machine/OS underneath. – jbm Apr 09 '16 at 18:53
  • (also: the setup of an IP adress to an interface is in any decent OS a privileged admin operation, ex on Linux require CAP_NET_ADMIN or CAP_SYS_ADMIN = more or less "root"- and in my limited understanding of Java, again by design, for security reasons, it is not supposed to do such things) – jbm Apr 09 '16 at 19:01
  • ...and last: now, there may be some libs (if that's the correct vocabulary for Java), with proper JNI and C/C++ below, even with a limited OS diversity range, that may help you? Actually I'd be surprise if not, because that's a very real need. – jbm Apr 09 '16 at 19:03
  • You can check if my assumption of "because no IP" is correct by setting yourself a dummy fixed IP to it (on the command line or via your OS GUI), say 192.168.42.42, and see if that change something. There's no need for it to be connected to any network: that's a whole different level, with association, authentication for 802.11, and then usually a DHCP process for IP, which are 2 completely different things. Wi-Fi interfaces are not "magic": you can give them an IP manually. Even with no actual network behind. – jbm Apr 09 '16 at 19:12
  • Are you experiencing an off-by-one? Try changing from a `while` loop to a `do-while` loop and see if it works. – Joel C Apr 10 '16 at 00:20
  • I think you cannot see it using NetworkInterface. The response to you question is here: http://stackoverflow.com/questions/17698971/networkinterface-getnetworkinterfaces-not-listing-all-interfaces – rodolk Apr 11 '16 at 03:48

0 Answers0