2010-01-05 75 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中“属性”选项卡上的“编译时保存”选项为我解析了类似的错误消息。