InetAddress.getLocalHost()在linux中抛出unknownHostException

InetAddress.getLocalHost()在linux中抛出unknownHostException,直到我在/ etc / hosts中手动添加条目。有没有办法获得InetAddress对象而不在/ etc / host文件中添加条目。注意:IP是静态的     
已邀请:
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();
                    }
                  }
    
我想知道同样的事情。在这篇文章中,我有一台机器,我没有在/ etc / hosts中添加一个条目...... java getLocalHost()UnknownHostException / etc / hosts文件与linux api不同? 但我认为该机器可能配置不同,但不确定如何。我不想将它添加到/ etc / hosts,因为我们已经在/ etc / sysconfig / network中配置了主机名,并且“应该”就足够了。     

要回复问题请先登录注册