开发者

InetAddress.getLocalHost() throws unknownHostException in linux

开发者 https://www.devze.com 2023-01-07 17:00 出处:网络
InetAddress.getLocalHost() throws unknownHostException in linux u开发者_开发百科ntil I manually add entry in /etc/hosts.Is there any way to get InetAddress object without add an entry in /etc/host fil

InetAddress.getLocalHost() throws unknownHostException in linux u开发者_开发百科ntil 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


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();
                    }
                  }


I am wondering the same thing. In this post, I have a machine where I did NOT add an entry into /etc/hosts...

java getLocalHost() UnknownHostException /etc/hosts file differs linux api?

but I think that machine is maybe configured differently, but not sure how. I prefer not having to add it to /etc/hosts as we already configured the hostname in /etc/sysconfig/network and one place "should" be enough.

0

精彩评论

暂无评论...
验证码 换一张
取 消