InetAddress.getLocalHost()抛出UnknownHostException [英] InetAddress.getLocalHost() throws UnknownHostException

查看:226
本文介绍了InetAddress.getLocalHost()抛出UnknownHostException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在不同的操作系统上测试我们的服务器应用程序(编写的Java),并认为OpenSolaris(2008.11)由于良好的Java集成而成为最不麻烦的。结果我错了,因为我最终得到了一个UnknownHostException

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();
}

输出为:

java.net.UnknownHostException: desvearth01: desvearth01
    at java.net.InetAddress.getLocalHost(InetAddress.java:1353)

然而, nslookup desvearth01 返回正确的IP地址, nslookup localhost 按预期返回 127.0.0.1 。此外,相同的代码在FreeBSD上完美运行。我不知道OpenSolaris有什么特别之处吗?

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?

感谢任何提示,谢谢。

推荐答案

在传统上,我可以再次回答我自己的问题:

In good tradition I can answer my own question once again:

似乎 InetAddress.getLocalHost() 忽略/etc/resolv.conf但只查看/ etc / hosts文件(除了localhost之外我没有指定任何内容)。将IP和主机名添加到此文件可以解决问题并且异常消失。

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.

以上答案几乎是正确的我从上面得到提示,我的问题得到解决......谢谢。

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.

步骤:


  • 打开/等/ hosts,条目可能如下所示。

  • 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


  • 您需要再添加一行以上任何编辑如vi或gedit。
    < your-machine-ip> <您 - 机 - 名称> 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
    




    • 只需保存并重新运行你的java ...代码你的工作已完成。

    • 这篇关于InetAddress.getLocalHost()抛出UnknownHostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆