简体   繁体   中英

InetAddress java 8 is not getting the hostname

InetAddress.getLocalHost().getHostName() is no more getting the name of the HostName since I switched to java 8 ...

with the jdk1.8 the InetAddress.getLocalHost().getHostName() returns "localhost". Before (when I was using jdk1.6) it gives me the right hostname (which is "ACTION03") according to the network config :

cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=ACTION03

any help ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM