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

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

outputs on my Win2000 machine:

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

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

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