线程“main”中的java异常java.net.UnknownHostException:测试:测试:未知错误OS ubuntu

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24612100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:28:51  来源:igfitidea点击:

java Exception in thread "main" java.net.UnknownHostException: Test: Test: unknown error OS ubuntu

javaubuntu

提问by user3608352

This error is related to my previous question where I had an error with InetAddress.getLocalHost(). I found a suggestionto add an entry in /etc/hosts:

此错误与我之前的问题有关,其中InetAddress.getLocalHost(). 我找到了在 中添加条目 的建议/etc/hosts

myip     localhost
127.0.0.1   localhost
127.0.1.1   test5

but my error is still not resolved.

但我的错误仍未解决。

My code :

我的代码:

import java.net.*;

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

    }
}

Error :

错误 :

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.lookupAllHostAddr(InetAddress.java:907)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1302)
    at java.net.InetAddress.getLocalHost(InetAddress.java:1479)
    ... 1 more

采纳答案by hutingung

Your host name is Sachin. The exception in thread "main" java.net.UnknownHostException: Sachin: Sachin: unknown errorshow that.

您的主机名为 Sachin。该exception in thread "main" java.net.UnknownHostException: Sachin: Sachin: unknown error证明。

Add Sachin to /etc/hosts

将 Sachin 添加到 /etc/hosts

myip     localhost
127.0.0.1   localhost
127.0.1.1   test5
127.0.0.1   Sachin

回答by Dynamic Remo

There's a chance that you might be contacting to an external ip address in your application and you are not connected to the internet. So, Check your Internet Connection before making some change to host/dns or any other...

有可能您正在联系应用程序中的外部 IP 地址,而您没有连接到互联网。因此,在对主机/dns 或任何其他更改进行一些更改之前,请检查您的 Internet 连接...

There's also a possibility that you require a VPN Connection to be in the some particular network to access those external url/ip. So, check that too.

也有可能您需要某个特定网络中的 VPN 连接才能访问这些外部 url/ip。所以,也要检查一下。

Cheers ;-)

干杯;-)