9

What is the order in which NetworkInterface.getNetworkInterfaces() returns an enumeration of network interfaces? Is there a way to affect that on JVM level or on Linux OS level?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Andrey Adamovich
  • 20,285
  • 14
  • 94
  • 132
  • The enumeration is based on the result of a call to a native method (in openJDK1.6): [getNetworkInterfaces](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/net/NetworkInterface.java#NetworkInterface.getNetworkInterfaces%28%29) – Andreas Dolk May 19 '11 at 08:18
  • I don't know the answer to your question, but I'm curious why you would like to change the order? – MarcoS May 19 '11 at 08:19
  • is there a way to affect that on Linux level? – Andrey Adamovich May 19 '11 at 08:20
  • @MarcoS, because my application server (weblogic) chooses wrong ip address as default one and I know it uses NetworkInterface.getNetworkInterfaces() to get a list of local interfaces, and it starts listeners on all of them. I still want it to listen to all local addresses, but choose different one as a default one. – Andrey Adamovich May 19 '11 at 08:22
  • @MarcoS, and default one seems to be the first in the list returned by getNetworkInterfaces() method. – Andrey Adamovich May 19 '11 at 08:23
  • @Andrey: each sane server should have a way to specify the IP address to bind to. I'm sure WebLogic has a way to do that. – Joachim Sauer May 19 '11 at 08:29
  • Then why not http://download.oracle.com/docs/cd/E13222_01/wls/docs81/adminguide/network.html#1171513 and force the poor thing. Or google for weblogic listen address - lots of advice. – MJB May 19 '11 at 08:31
  • @Joachim Sauer, weblogic allows to specify only one listen address, but I need that to listen to all local addresses and that's possible when listen address is empty. If I specify the listen adress I want, then some other things break because localhost is not available anymore. So, that's why I wanted to modify the order to just let weblogic live with different default (first) address, but still listen to all. – Andrey Adamovich May 19 '11 at 08:35
  • @MJB, that's what I did before posting this question. I spend about a day trying to configure different listen address and network channels, but it does not work in the same way as when listen address is not specified (i.e. that means all local addresses are being listened to). I'm also awaiting Oracle response on a support case on that, but in a meanwhile I wanted to find simpler solution. – Andrey Adamovich May 19 '11 at 08:37
  • 2
    Well, not to sound persnickety, but wouldn't it have been nice to clearly state all this :) – MJB May 19 '11 at 08:38
  • @MJB, I don't think so, the question is rather generic and stating all the background details does not change the answer to the question. For example, if I have some Java FX related question, do you really care why on earth I use Java FX? – Andrey Adamovich May 19 '11 at 08:43
  • But you divided the question. You have a separate question on WebLogic and listener addresses. But why don't each of them LINK to each other??? – MJB May 19 '11 at 08:44
  • @MJB, they are related for me, but not neccessarily for anyone else. As I said this question is generic and just wanted to know if it is possible to affect the order. Maybe someone else who uses getNetworkInterfaces for something else will come over this question and it will help him in some way. If I attach all unneeded details, it will only create unneeded noice around the question. – Andrey Adamovich May 19 '11 at 08:49
  • @Andrey: you've run into a common mistake here: you're asking about a **specific attempted solution** without telling us what the overarching problem is. This **only** serves to limit the set of possible answers and adds no benefit whatsoever. *Some* context is *always* useful. It's usually more production to describe the **problem** instead of (or in addition to) the intended solution. – Joachim Sauer May 19 '11 at 08:55
  • @Joachim, I agree to you in general, but in this specific case I don't think it helps. Actually, I have asked a question about overarching problem (http://stackoverflow.com/questions/6055153/set-preffered-listen-address-in-weblogic-11g), but that question drew very little attention. But this more generic question at least got clear answers from several people. So, IMHO details not always help, but I agree that in many cases details are helpful. – Andrey Adamovich May 19 '11 at 09:08

3 Answers3

6

According to the source of the OpenJDK (found in src/solaris/native/java/net/NetworkInterface.c, method enumInterfaces) it will return IPv4 interfaces first (method enumIPv4Interfaces), followed by IPv6 interfaces (method enumIPv6Interfaces).

The order within those categories seems to be the same that the OS uses (it uses the SIOCGIFCONF ioctl).

Note that this is implementation dependent and not defined, so any implementation can very easily do it differently.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

This simply delegates to a native call, and no I'm not aware of any way to alter it.

MJB
  • 9,352
  • 6
  • 34
  • 49
  • 1
    but the question was about how to affect the order, I guess if you don't know the answer there is no big reason to answer. – Andrey Adamovich May 19 '11 at 08:25
  • My point was it is obviously implementation dependent, since neither the external javadocs nor the source indicate a specific order. And no system properties were used/passed. So yes, I think there is relevance to my answer – MJB May 19 '11 at 08:28
  • @Andrey - if you want people to bother to answer your questions, don't criticize their answers. – Stephen C May 19 '11 at 09:19
1

If you take a look at sources, then you see that getNetworkInterfaces just return enumeration, which backed with a NetworkInterface array, which is returned by getAll() method, which is native. So, it is implementation dependent and system dependent. You can't do anything with this.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103