tags:

views:

86

answers:

1

InetAddress.getLocalHost() throws unknownHostException in linux until I manually add entry in /etc/hosts. Is there any way to get InetAddress object without add an entry in /etc/host file.. Note : The IP is static

A: 
String host = null;
NetworkInterface iface = null;

        for(Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
                   iface = (NetworkInterface)ifaces.nextElement();
                   InetAddress ia = null;
                    for(Enumeration<InetAddress> ips = iface.getInetAddresses();ips.hasMoreElements();){
                    ia = (InetAddress)ips.nextElement();
                    if(!ia.isLoopbackAddress() && (!ia.isLinkLocalAddress()) && (ia instanceof Inet4Address)) host=ia.getHostAddress();
                    }
                  }