java - InetAddress.getLocalHost() throws unknownHostException in linux -


InetAddress.getLocalHost () throws anonymousHost extension to Linux unless I manually enter it in / etc / hosts Will do. No way to get an InetAddress object to add an entry in the / etc / host file. Note: The IP is static

  string host = null; Network interface iface = null; (Enumeration & lt; network interface & gt; ifaces = NetworkInterface.getNetworkInterfaces (); ifaces.hasMoreElements ();) {iface = (NetworkInterface) ifaces.nextElement (); InetAddress ia = null; For (counting & lt; InetAddress & gt; ips = iface.getInetAddresses (); ips.hasMoreElements ();) {ia = (InetAddress) ips.nextElement (); If (! Ia.isLoopbackAddress () & amp; amp; (! Ia.isLinkLocalAddress ()) & amp;; (Inet4Address for example)) host = ia.getHostAddress (); }}  

Comments