InetAddress.getLocalHost() 抛出 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();
}
输出是:
java.net.UnknownHostException: desvearth01: desvearth01
at java.net.InetAddress.getLocalHost(InetAddress.java:1353)
但是,nslookup desvearth01
返回正确的 IP 地址,nslookup localhost
返回 127.0.0.0。 0.1 正如预期的那样。此外,相同的代码在 FreeBSD 上也可以完美运行。 OpenSolaris 有什么我不知道的特别之处吗?
任何提示表示赞赏,谢谢。
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
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?
Any hints appreciated, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
按照良好的传统,我可以再次回答我自己的问题:
似乎
InetAddress.getLocalHost()
忽略了/etc/resolv.conf
,但只查看 < code>/etc/hosts 文件(除了localhost
之外,我没有指定任何内容)。将IP和主机名添加到该文件中解决了问题并且异常消失了。另一个答案几乎是正确的,我从上面得到了提示,我的问题得到了解决......谢谢。
但为了改进这一点,我添加了逐步的更改,这样即使是天真的用户也会有所帮助。
步骤:
打开
/etc/hosts
,条目可能如下所示。<预><代码> 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 本地主机 本地主机.本地域 本地主机6 本地主机6.本地域6
您需要使用任何编辑器(例如
vi
或gedit
)在上面添加一行(例如-机器名称>本地主机
)。<前> <代码> 192.168.1.73 my_foo 本地主机
现在,整个文件可能如下所示:
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 besideslocalhost
). Adding the IP and hostname to this file solves the problem and the exception is gone.Another 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 helpful for even naive users.
Steps:
Open
/etc/hosts
, the entries might look like below.You need to add one more line above of this by any editor like
vi
orgedit
(e.g.<your-machine-ip> <your-machine-name> localhost
).Now, overall file may look like this:
我使用
NetworkInterface.getNetworkInterfaces ()
作为InetAddress.getLocalHost()
抛出UnknownHostException
。这是代码(为了清楚起见,没有进行异常处理)。其他答案编辑
/etc/hosts
文件。这很容易出错,很脆弱,可能需要 root 访问权限,并且不适用于所有操作系统。I use
NetworkInterface.getNetworkInterfaces()
as a fall back for whenInetAddress.getLocalHost()
throws anUnknownHostException
. Here's the code (without exception handling for clarity).Other answers edit the
/etc/hosts
file. This is error prone, brittle, may require root access and won't work on all OS's.在我的亚马逊实例上,我遇到了同样的问题,存在默认 DNS 配置问题。所以为了解决这个问题,我做了这些步骤 -
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 -
Solaris 上的主机查找使用
/etc/nsswitch.conf
,因此根据“hosts:”行的内容确定是否/etc/hosts
、NIS、DNS 和/或应咨询 LDAP。如果您只使用主机和 DNS,您应该在
/etc/nsswitch.conf
中包含以下内容:nslookup desvearth01
起作用的原因是nslookup
命令直接查阅/etc/resolv.conf
。如果你想做更好的命令行测试,使用命令: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.If you only use hosts and DNS you should have this in
/etc/nsswitch.conf
:The reason
nslookup desvearth01
works is because thenslookup
command directly consults/etc/resolv.conf
. If you want to do a better command line test, use the command:当我更改工作站名称并尝试启动 Glassfish 2 时,会出现此错误。您还必须重命名 /etc/hosts 中的条目,如下所示:
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:
签出/etc/hostname,然后将您的主机名放入主机文件中。
Checkout /etc/hostname then put your hostname to hosts file.
另一个选项在这篇文章中(事实上,您的主机名的 /etc/sysconfig/network 文件中的内容...通过将其更改为 FQDN 名称可以修复此问题)。
java getLocalHost() UnknownHostException /etc/hosts 文件与 Linux api 不同?
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).
java getLocalHost() UnknownHostException /etc/hosts file differs linux api?
如果您看到此消息,则需要设置主机名WITH
hostname superhost.domain
COMMAND!之后,检查哪个
/etc/hosts
文件包含类似127.0.0.1 localhost
的字符串。另外,检查命令
uname -a
返回类似以下内容:不是这样的!!!!!< /强>
If you see this message than you need set hostname WITH
hostname superhost.domain
COMMAND!After this, check which
/etc/hosts
file contain string like this127.0.0.1 localhost
.Also, check that command
uname -a
returns something like this:NOT LIKE THIS!!!!
我也有这方面的问题。我需要做进一步的测试,但看起来像
NetworkInterface.getNetworkInterfaces()
可以更可靠。我认为这并不进行查找,而只是获取IP。当 getLocalHost() 失败时,我将其用作“下一个最佳”。
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.I am using it as the 'next best' when the
getLocalHost()
fails.