2012-02-16 64 views
3

我試圖使用教程讀取URL:http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.htmlJava教程 - 未知的主機異常

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) 
+4

「谷歌」的網址不喜歡被稱爲「oracle」:) – 2012-02-16 12:20:15

回答

3

您是否在代理服務器後運行? 該文章似乎暗示在這種情況下需要配置。

或者,程序可能會掛起,或者您可能會看到異常堆棧跟蹤。如果發生後兩個事件中的任何一個,您可能必須設置代理主機,以便程序可以找到Oracle服務器。

2

我認爲它的網絡錯誤檢查您的網絡連接....

+2

它將更適合作爲評論TMHO。對於提問者:嘗試與另一個網站運行相同的代碼,你是否仍然遇到同樣的錯誤? – alfasin 2012-02-17 02:29:52

3

你身後的代理?嘗試在eclipse中設置代理或嘗試

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