4

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 ?

4
  • What does hostname give you? Commented Jul 28, 2016 at 4:22
  • @Robert it gives me ACTION03 Commented Jul 28, 2016 at 4:25
  • You had two host entries for localhost in your host file prior to edit. Not sure if that has something to do with it. Perhaps without one to one mapping, it gives localhost? Would need to test it. Unsure of the correct behavior, but it otherwise works for me. Commented Jul 28, 2016 at 13:10
  • What is the content of the /etc/hosts file? Commented Jul 28, 2016 at 15:38

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You made my day!
1

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

1 Comment

Me I have Centos 7 ! And it gives me localhost!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.