InetAddress.getLocalHost()在运行嵌入式Java的机器上失败

时间:2017-03-28 09:20:08

标签: java linux networking

在这篇文章中java H2 hanging on getLocalhost on arm32 device我在使用Hibernate创建数据库时遇到了问题。我现在设法使用此测试程序删除跟踪问题到核心java,使用getByName(" localhost")的InetAddress查找工作,但getLocalHost()失败!

指向linux机器的配置问题。 localhost位于/​​ etc / hosts

但我不是Linux专家,是什么导致这个?

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Addresses
{
     public static void main(String[] args) {

        try {
           InetAddress address= InetAddress.getByName("localhost");
           System.out.println(address.getHostName()+ "-"+address.getHostAddress());

           address= InetAddress.getLocalHost();
           System.out.println(address.getHostName()+ "-"+address.getHostAddress());

        } catch (UnknownHostException e) {
           e.printStackTrace();
        }
     }
}

给出

localhost-127.0.0.1
java.net.UnknownHostException: N1-ZS10: N1-ZS10: Name or service not known
        at java.net.InetAddress.getLocalHost(InetAddress.java:1505)
        at Addresses.main(Addresses.java:12)
Caused by: java.net.UnknownHostException: N1-ZS10: Name or service not known
        at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
        at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
        at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
        at java.net.InetAddress.getLocalHost(InetAddress.java:1500)
        ... 1 more

0 个答案:

没有答案