InetAddress.getLocalHost() 抛出 UnknownHostException

发布于 2024-08-13 17:49:19 字数 774 浏览 11 评论 0原文

我正在不同的操作系统上测试我们的服务器应用程序(用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

唔猫 2024-08-20 17:49:19

按照良好的传统,我可以再次回答我自己的问题:

似乎 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

  • 您需要使用任何编辑器(例如 vigedit)在上面添加一行(例如 -机器名称>本地主机)。

    <前> <代码> 192.168.1.73 my_foo 本地主机

现在,整个文件可能如下所示:

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 代码...您的工作就完成了。

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.


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.

     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 (e.g. <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.
笑饮青盏花 2024-08-20 17:49:19

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

其他答案编辑 /etc/hosts 文件。这很容易出错,很脆弱,可能需要 root 访问权限,并且不适用于所有操作系统。

I use NetworkInterface.getNetworkInterfaces() as a fall back for when InetAddress.getLocalHost() throws an UnknownHostException. Here's the code (without exception handling for clarity).

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

Other answers edit the /etc/hosts file. This is error prone, brittle, may require root access and won't work on all OS's.

时光倒影 2024-08-20 17:49:19

在我的亚马逊实例上,我遇到了同样的问题,存在默认 DNS 配置问题。所以为了解决这个问题,我做了这些步骤 -

获取您的主机名

$hostname
ip-10-122-16-169

ping 主机名

$ping ip-10-122-16-169
ping: unknown host ip-10-122-16-169

cat /etc/hosts 文件,你会得到类似的内容

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost6 localhost6.localdomain6

现在您只需在第一行末尾附加您的主机名,因此当您附加时它看起来像

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 ip-10-122-16-169
::1         localhost6 localhost6.localdomain6

现在您已准备好,再次检查同一主机名的 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

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 -

get your host name

$hostname
ip-10-122-16-169

ping to hostname

$ping ip-10-122-16-169
ping: unknown host ip-10-122-16-169

cat /etc/hosts file, you will get something like

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 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
我ぃ本無心為│何有愛 2024-08-20 17:49:19

Solaris 上的主机查找使用 /etc/nsswitch.conf,因此根据“hosts:”行的内容确定是否 /etc/hosts、NIS、DNS 和/或应咨询 LDAP。

如果您只使用主机和 DNS,您应该在 /etc/nsswitch.conf 中包含以下内容:

hosts: files dns

nslookup desvearth01 起作用的原因是 nslookup 命令直接查阅/etc/resolv.conf。如果你想做更好的命令行测试,使用命令:

getent hosts desvearth01

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:

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:

getent hosts desvearth01
情泪▽动烟 2024-08-20 17:49:19

当我更改工作站名称并尝试启动 Glassfish 2 时,会出现此错误。您还必须重命名 /etc/hosts 中的条目,如下所示:

127.0.0.1       localhost
127.0.1.1       MyNewName

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:

127.0.0.1       localhost
127.0.1.1       MyNewName
弥繁 2024-08-20 17:49:19

签出/etc/hostname,然后将您的主机名放入主机文件中。

Checkout /etc/hostname then put your hostname to hosts file.

桃酥萝莉 2024-08-20 17:49:19

另一个选项在这篇文章中(事实上,您的主机名的 /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?

没有心的人 2024-08-20 17:49:19

如果您看到此消息,则需要设置主机名WITHhostname superhost.domainCOMMAND

之后,检查哪个 /etc/hosts 文件包含类似 127.0.0.1 localhost 的字符串。

另外,检查命令 uname -a 返回类似以下内容:

Linux superhost.domain 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

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 this 127.0.0.1 localhost.

Also, check that command uname -a returns something like this:

Linux superhost.domain 2.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

那一片橙海, 2024-08-20 17:49:19

我也有这方面的问题。我需要做进一步的测试,但看起来像
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文