java教程 - 未知主机异常

时间:2012-02-16 12:14:38

标签: java

我正在尝试使用教程阅读网址:http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html

import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {

        URL oracle = new URL("http://www.google.com/");
        BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);

        in.close();
    }

}

但我已经超越了例外。 www.google.com是众所周知的主持人?

Exception in thread "main" java.net.UnknownHostException: www.google.com
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:234)
    at sun.net.www.http.HttpClient.New(HttpClient.java:307)
    at sun.net.www.http.HttpClient.New(HttpClient.java:324)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
    at java.net.URL.openStream(URL.java:1010)
    at URLReader.main(URLReader.java:8)

3 个答案:

答案 0 :(得分:3)

你是代理人吗?尝试在eclipse中设置代理或尝试

 java -DproxySet=true -DproxyHost=10.0.0.14 -DproxyPort=6588 JavApp

答案 1 :(得分:3)

你是在代理后面跑吗? 该文似乎表明在这种情况下需要配置。

  

或者,程序可能会挂起,或者您可能会看到异常堆栈跟踪。如果后两个事件中的任何一个发生,您可能必须设置代理主机,以便程序可以找到Oracle服务器。

答案 2 :(得分:2)

我认为它的网络错误会检查您的互联网连接....