Exception in thread "main" java.net.UnknownHostException: pj-pc.m.c.t: pj-pc.m.c.t: nodename nor servname provided, or not known
at java.net.InetAddress.getLocalHost(InetAddress.java:1473)
at net.muzee.paid.channels.Foo.main(Foo.java:8)
Caused by: java.net.UnknownHostException: pj-pc.m.c.t: nodename nor servname provided, or not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
at java.net.InetAddress.getLocalHost(InetAddress.java:1469)
... 1 more
把問題最小化後,可以用這樣的 code 來重現:package net.muzee.paid.channels;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostTest {
public static void main(String[] args) throws UnknownHostException {
String localHostname = InetAddress.getLocalHost().getHostName();
System.out.println(localHostname);
}
}
看起來是在 osx 10.9 預設是沒有設 localhost 名稱,而 JDK 因為取不到名為 localhost 的記錄,就繼續正常的流程,取 domain 然後進行驗證。最後得出 UnknownHostException
的結論。在一篇文章中找到,如何改變 osx 的 localhost 名稱。設定前是沒有值的情況:
qty:~ qrtt1$ scutil --get HostName
HostName: not set
設定完成後,就可以看到 localhost 字樣(如果你有其它需要,也能設成不同的值):qty:~ qrtt1$ scutil --set HostName 'localhost'
qty:~ qrtt1$ scutil --get HostName
localhost