InetAddress java 8 is not getting the hostname

爷,独闯天下 提交于 2019-12-01 06:51:14

Works for me on Linux (Ubuntu 14.04) with Java 1.8.0_05.

public class HostName {
    public static void main(String[] args) throws Exception {
        System.out.println(java.net.InetAddress.getLocalHost().getHostName());
    }
}

robert@habanero:~$ javac HostName.java && java HostName

habanero

There was similar bug fired in JDK.

What I understand is that they changed default resolution process.

They honor configuration in /etc/nsswitch.conf where hosts are configured for /etc/hosts that gives it main priority for name resolution.

Usually /etc/hosts has record for 127.0.0.1 localhost that provide name for host localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!