9

It works okay for me but on OSX Mavericks for a particular customer InetAddress.getLocalHost() is failing with Java 7 although it works okay with Java 6 with the following exception

java.net.UnknownHostException: rupert: rupert: nodename nor servname provided, or not known
    at java.net.InetAddress.getLocalHost(InetAddress.java:1466)
Caused by: java.net.UnknownHostException: rupert: nodename nor servname provided, or not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:894)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1286)
    at java.net.InetAddress.getLocalHost(InetAddress.java:1462)

Is this by design ?

Update

Looks to be a known bug fixed in Java 8

https://bugs.openjdk.java.net/browse/JDK-7180557

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • 2
    I'd say this is environmental. Their hostname is being looked up in DNS and is not being found. It does work in the Java 6 VM but obviously the lookup mechanism for Java 7 has changed. if I change my hostname using `sudo hostname rupert`, and then run some simple test code I get the same error. – Anya Shenanigans Nov 08 '13 at 17:17
  • 1
    +1 to the comment that this is a known bug in Java that was fixed in 1.8. I can confirm that running this under Oracle Java 1.7.0_72 (latest as of this writing) on my Mac OS X 10.10 (Yosemite) environment fails with the reported exception, but as expected under Oracle Java 1.8.0_25 (latest as of this writing). I believe it was working properly under Mac OS X 10.9 (Mavericks) before I upgraded. I suspect that the upgrade tweaked something very particular about the networking configuration since the original report mentions Mavericks and not Yosemite. – Christopher Schultz Nov 11 '14 at 18:00
  • Is it expected for the fix to be backported to those who require JDK 7 rather than 8? I do not see this mentioned in the bug reported – Idan Adar Dec 18 '14 at 18:53
  • From that bug report appears it was also fixed in 7u85 but then it appears they only released up through 7u79 publically . – rogerdpack May 18 '16 at 18:26

2 Answers2

7

Add the {yourhostname} part of this to /etc/hosts

e.g.

127.0.0.1       localhost {yourhostname}

and the problem disappears.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
cersgde
  • 71
  • 1
  • 2
  • 3
    You can get your hostname by entering the command `hostname` in terminal and seeing what it returns. – Gray Nov 05 '15 at 22:30
  • Finally an answer that at least works around the problem, thanks! (as a note for followers, this I believe is related to the fact that its doing some kind of reverse hostname lookup http://apple.stackexchange.com/q/30552/25085) – rogerdpack May 18 '16 at 18:29
4

I am guessing you will find the answer here - even though that question is about Solaris, both Mac OS X and Solaris are Unix operating systems.

Community
  • 1
  • 1
Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • Yes, you are right I suppose this begs the question how do you get the local ipa address in Java then, and also how do you get the mac address as I was using InetAddress address = java.net.InetAddress.getLocalHost(); NetworkInterface ni = NetworkInterface.getByInetAddress(address); – Paul Taylor Nov 10 '13 at 22:44
  • If this is a duplicate question you should vote to close, not link to the duplicate in an answer. – bhspencer May 18 '16 at 18:45
  • Even if I don't know if it is actually a duplicate? It was just a guess, otherwise I would have voted. – Robin Green May 20 '16 at 21:22