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.
精彩评论