Getting the list of network interfaces installed on your machine

If you have multiple IP addresses on one machine, it may be useful to specify which one to use for your networking stuff. Since JDK1.4, you can enumerate them and select one with the class java.net.NetworkInterface.

Main.java:

import java.util.*;
import java.net.*;
 
public class Main
{
   public static void main(String []args) {
      try {
         Enumeration enum = NetworkInterface.getNetworkInterfaces();
         while (enum.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) enum.nextElement();
            System.out.println(ni);
         }
      }
      catch(SocketException e) {
         e.printStackTrace();
      }
   }
}

outputs on my Win2000 machine:

name:lan0 (Intel DC21140 PCI Fast Ethernet Adapter) index: 1 addresses:
/192.168.2.10;

name:lan1 (Intel DC21140 PCI Fast Ethernet Adapter) index: 2 addresses:

name:lo0 (MS TCP Loopback interface) index: 3 addresses:
/127.0.0.1;