2010-01-05 48 views
0

只是玩java試圖學習它等在簡單的Java應用程序中的錯誤

這是我的代碼到目前爲止,使用HtmlUnit。

package hsspider; 

import com.gargoylesoftware.htmlunit.WebClient; 

/** 
* @author 
*/ 
public class Main { 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     System.out.println("starting "); 
     Spider spider = new Spider(); 
     spider.Test(); 
    } 
} 


package hsspider; 

import com.gargoylesoftware.htmlunit.WebClient; 
import com.gargoylesoftware.htmlunit.html.HtmlPage; 
/** 
* @author 
*/ 
public class Spider { 

    public void Test() throws Exception 
    { 
     final WebClient webClient = new WebClient(); 
     final HtmlPage page = webClient.getPage("http://www.google.com"); 
     System.out.println(page.getTitleText()); 
    } 
} 

我正在使用Netbeans。

我似乎無法弄清楚問題是什麼,爲什麼不編譯?

錯誤:

C:\Users\mrblah\.netbeans\6.8\var\cache\executor-snippets\run.xml:45: 
Cancelled by user. 
BUILD FAILED (total time: 0 seconds) 

在XML中的行:

<translate-classpath classpath="${classpath}" targetProperty="classpath-translated" /> 

回答

5

測試聲明拋出異常。如果你添加「拋出異常」到你的主要方法,它應該編譯。例如:

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws Exception { 
    System.out.println("starting "); 
    Spider spider = new Spider(); 
    spider.Test(); 
} 
+0

是不是在主要方法而不是拋出異常,而不是風格?爲什麼不抓住異常並輸出錯誤信息? – NomeN 2010-04-17 21:16:57

+0

確定這是一種不好的風格,但對於一個只想知道爲什麼他們的代碼無法工作的人來說,這當然足夠了。 – 2010-04-19 21:26:53

1

史蒂夫說的是正確的。但是,Test的大寫字母可能存在一些問題。一種方法總是以小寫字符開頭。所以test會更好。

+1

方法總是以小寫字母*開頭*。這不是必需的。例如,一些約定具有以下劃線開頭的私有方法。 – 2010-01-05 19:09:53

1

取消選中Netbeans 7.1.2中「屬性」選項卡上的「編譯時保存」選項爲我解析了類似的錯誤消息。

相關問題