Java InetAddress.getLocalHost() 抛出 UnknownHostException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1881546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
InetAddress.getLocalHost() throws UnknownHostException
提问by jhwist
I am testing our server-application (written Java) on different operating systems and thought that OpenSolaris (2008.11) would be the least troublesome due to the nice Java integration. Turns out I was wrong, as I end up with a UnknownHostException
我正在不同的操作系统上测试我们的服务器应用程序(编写的 Java),并认为 OpenSolaris (2008.11) 将是最不麻烦的,因为它具有良好的 Java 集成。结果我错了,因为我最终遇到了 UnknownHostException
try {
computerName = InetAddress.getLocalHost().getHostName();
if (computerName.indexOf(".") > -1)
computerName = computerName.substring(0,
computerName.indexOf(".")).toUpperCase();
} catch (UnknownHostException e) {
e.printStackTrace();
}
The output is:
输出是:
java.net.UnknownHostException: desvearth01: desvearth01
at java.net.InetAddress.getLocalHost(InetAddress.java:1353)
However, nslookup desvearth01
returns the correct IP address, and nslookup localhost
returns 127.0.0.1
as expected. Also, the same code works perfectly on FreeBSD. Is there anything special to OpenSolaris that I am not aware of?
但是,nslookup desvearth01
返回正确的 IP 地址,并按预期nslookup localhost
返回127.0.0.1
。此外,相同的代码在 FreeBSD 上也能完美运行。OpenSolaris 有什么我不知道的特别之处吗?
Any hints appreciated, thanks.
任何提示表示赞赏,谢谢。
采纳答案by jhwist
In good tradition I can answer my own question once again:
按照传统,我可以再次回答我自己的问题:
It seems that InetAddress.getLocalHost()
ignores the /etc/resolv.conf but only looks at the /etc/hosts file (where I hadn't specified anything besides localhost). Adding the IP and hostname to this file solves the problem and the exception is gone.
似乎InetAddress.getLocalHost()
忽略了 /etc/resolv.conf 但只查看 /etc/hosts 文件(除了本地主机之外我没有指定任何内容)。将 IP 和主机名添加到此文件即可解决问题,异常消失。
Above answer is almost correct and I got hint from above and my problem get resolved...Thanks.
上面的答案几乎是正确的,我从上面得到了提示,我的问题得到了解决......谢谢。
But to improve this, I am adding steps-by-steps changes, so that it will be helpfull for even naive users.
但是为了改进这一点,我正在添加分步更改,以便即使是天真的用户也会有所帮助。
Steps:
脚步:
Open /etc/hosts, the entries might look like below.
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
you need to add one more line above of this by any editor like vi or gedit.
<your-machine-ip> <your-machine-name> localhost
eg.192.168.1.73 my_foo localhost
打开 /etc/hosts,条目可能如下所示。
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
您需要通过任何编辑器(如 vi 或 gedit)在此之上再添加一行。
<your-machine-ip> <your-machine-name> localhost
例如。192.168.1.73 my_foo localhost
Now, overall file may look like this:
现在,整个文件可能如下所示:
192.168.1.73 my_foo localhost
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- Just save it and run again your java...code your work is done.
- 只需保存并再次运行你的 java...code 你的工作就完成了。
回答by Martin
Host lookups on Solaris uses /etc/nsswitch.conf
so depending on what the 'hosts:' line says it determines if /etc/hosts
, NIS, DNS and/or LDAP should be consulted.
Solaris 上的主机查找的使用/etc/nsswitch.conf
取决于“hosts:”行的内容,它确定是否/etc/hosts
应咨询 NIS、DNS 和/或 LDAP。
If you only use hosts and DNS you should have this in /etc/nsswitch.conf
:
如果你只使用主机和 DNS,你应该在/etc/nsswitch.conf
:
hosts: files dns
The reason nslookup desvearth01
works is because the nslookup
command directly consults /etc/resolv.conf
. If you want to do a better command line test, use the command:
之所以nslookup desvearth01
有效是因为该nslookup
命令直接咨询/etc/resolv.conf
. 如果你想做一个更好的命令行测试,使用命令:
getent hosts desvearth01
回答by Jon
I am having issues around this as well. I need to do further testing, but it looks like
NetworkInterface.getNetworkInterfaces()
can be more reliable. I think that this does not do the lookup, but just grabs the IP.
我也有这方面的问题。我需要做进一步的测试,但看起来
NetworkInterface.getNetworkInterfaces()
可以更可靠。我认为这不会进行查找,而只是获取 IP。
I am using it as the 'next best' when the getLocalHost()
fails.
当getLocalHost()
失败时,我将它用作“次优” 。
回答by Pavel
If you see this message than you need set hostname WITHhostname superhost.domain
COMMAND!
如果您看到此消息,则需要使用hostname superhost.domain
命令设置主机名!
After this, check which /etc/hosts
file contain string like this 127.0.0.1 localhost
.
在此之后,检查哪个/etc/hosts
文件包含这样的字符串127.0.0.1 localhost
。
Also, check that command uname -a
returns something like this:
另外,检查命令uname -a
返回如下内容:
Linux superhost.domain2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
Linux superhost.domain2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
NOT LIKE THIS!!!!
不喜欢这个!!!!
Linux (none)2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
Linux (无)2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
回答by Dean Hiller
Another option is in this post (in fact, what is in your /etc/sysconfig/network file for your hostname...by changing it to an FQDN name fixes this issue).
另一个选项是在这篇文章中(实际上,您的主机名在 /etc/sysconfig/network 文件中的内容...通过将其更改为 FQDN 名称可以解决此问题)。
java getLocalHost() UnknownHostException /etc/hosts file differs linux api?
java getLocalHost() UnknownHostException /etc/hosts 文件与 linux api 不同?
回答by user4877680
Checkout /etc/hostnamethen put your hostname to hosts file.
签出/etc/hostname然后将您的主机名放入主机文件。
回答by Nathan
I use NetworkInterface.getNetworkInterfaces()
as a fall back for when InetAddress.getLocalHost()
throws an UnknownHostException
. Here's the code (without exception handling for clarity).
我用NetworkInterface.getNetworkInterfaces()
作为当回落InetAddress.getLocalHost()
抛出UnknownHostException
。这是代码(为了清楚起见,没有异常处理)。
Enumeration<NetworkInterface> iterNetwork;
Enumeration<InetAddress> iterAddress;
NetworkInterface network;
InetAddress address;
iterNetwork = NetworkInterface.getNetworkInterfaces();
while (iterNetwork.hasMoreElements())
{
network = iterNetwork.nextElement();
if (!network.isUp())
continue;
if (network.isLoopback()) // If I want loopback, I would use "localhost" or "127.0.0.1".
continue;
iterAddress = network.getInetAddresses();
while (iterAddress.hasMoreElements())
{
address = iterAddress.nextElement();
if (address.isAnyLocalAddress())
continue;
if (address.isLoopbackAddress())
continue;
if (address.isMulticastAddress())
continue;
return(address.getHostAddress());
}
}
回答by Jimmy
This errors shows up when I changed the workstation name and tried start Glassfish 2. You also must rename the entry at /etc/hosts, something like this:
当我更改工作站名称并尝试启动 Glassfish 2 时,会出现此错误。您还必须重命名 /etc/hosts 中的条目,如下所示:
127.0.0.1 localhost
127.0.1.1 MyNewName
回答by ankit.vishen
On my amazon instance I was having the same issue, there was default DNS configuration issue. So to fix the issue I had done these steps -
在我的亚马逊实例上,我遇到了同样的问题,存在默认 DNS 配置问题。所以为了解决这个问题,我已经完成了这些步骤 -
get your host name
获取您的主机名
$hostname
ip-10-122-16-169
ping to hostname
ping 主机名
$ping ip-10-122-16-169
ping: unknown host ip-10-122-16-169
cat /etc/hostsfile, you will get something like
cat /etc/hosts文件,你会得到类似的东西
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost6 localhost6.localdomain6
now you just need to append your host name at the end of the fist line, so when you append it will look like
现在你只需要在第一行的末尾附加你的主机名,所以当你附加时它看起来像
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ip-10-122-16-169
::1 localhost6 localhost6.localdomain6
now you're ready to go, to check ping again the same hostname
现在你准备好了,再次检查 ping 相同的主机名
$ping ip-10-122-16-169
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=255 time=0.018 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=255 time=0.024 ms