线程“main” java.net.UnknownHostException: Test: Test: unknown error OS ubuntu 中的 java Exception

2022-09-03 13:13:39

此错误与我上一个问题有关,其中我遇到了错误。我发现了一个建议,在中添加一个条目:InetAddress.getLocalHost()/etc/hosts

myip     localhost
127.0.0.1   localhost
127.0.1.1   test5

但我的错误仍未解决。

我的代码 :

import java.net.*;

public class InetAddressTest {
    public static void main(String args[]) throws UnknownHostException {
        InetAddress Address = InetAddress.getLocalHost();

    }
}

错误:

Exception in thread "main" java.net.UnknownHostException: Sachin: Sachin: unknown error
    at java.net.InetAddress.getLocalHost(InetAddress.java:1484)
    at InetAddressTest.main(InetAddressTest.java:6)
Caused by: java.net.UnknownHostException: Sachin: unknown error
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:907)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1302)
    at java.net.InetAddress.getLocalHost(InetAddress.java:1479)
    ... 1 more

答案 1

您的主机名是 Sachin。显示那个。exception in thread "main" java.net.UnknownHostException: Sachin: Sachin: unknown error

将 Sachin 添加到 /etc/hosts

myip     localhost
127.0.0.1   localhost
127.0.1.1   test5
127.0.0.1   Sachin

答案 2

您可能会联系到应用程序中的外部IP地址,而您没有连接到互联网。因此,在对主机/ dns或任何其他...

还有一种可能性是,您需要VPN连接位于某个特定网络中才能访问这些外部URL / IP。因此,也请检查一下。

干杯;-)