我正在编写一个使用以下代码的ant任务:
public class Klazz extends Task{
public void execute() throws BuildException{
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.get("file:///C:/sample/alltests-fails.html");
}
在eclipse中,名为“ test-project ”的项目使用“ libs ”文件夹,其中包含将要添加到类路径中的jar( ant.jar,selenium-server-standalone-2.44.0.jar )。如果我在Eclipse中运行代码它工作正常但在作为ant任务运行时会抛出java.lang.ExceptionInInitializerError。下面是build.xml片段,用于创建jar(名为 custom-task.jar )文件,该文件需要放在%ant_home%\ lib文件夹中。
<target name="jar" depends="compile" >
<mkdir dir="build/jar" />
<jar destfile="${env.ANT_HOME}/lib/custom-task.jar">
<fileset dir="build/classes" />
<restrict>
<name name="**/*.class" />
<archives>
<zips>
<fileset dir="${basedir}/libs/" includes="**/*.jar" />
</zips>
</archives>
</restrict>
</jar>
</target>
在通过上面的“jar”任务创建jar时,可能是类路径中未正确添加的外部jar /类,导致一些缺少的类文件导致 ExceptionInInitializerError 。 感谢任何帮助。
下面的是堆栈跟踪:
java.lang.ExceptionInInitializerError
at org.cyberneko.html.HTMLScanner.scanEntityRef(HTMLScanner.java:1415)
at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2
059)
at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:920)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499
)
at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452
)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.pars
e(HTMLParser.java:926)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:2
45)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.ja
va:191)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(Defau
ltPageCreator.java:268)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPa
geCreator.java:156)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient
.java:455)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:329)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:394)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:4
77)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:4
66)
at mypkg.Klazz.execute(Klazz.java:15)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExe
cutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.Main.runBuild(Main.java:851)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at org.cyberneko.html.HTMLEntities.load0(HTMLEntities.java:101)
at org.cyberneko.html.HTMLEntities.<clinit>(HTMLEntities.java:53)
... 33 more
Total time: 2 seconds
答案 0 :(得分:1)
如果遇到任何课程加载困难,我会假设会出现NoClassDefFoundError
或ClassNotFoundException
ExceptionInInitializerError
通常不应该引起注意,因为它只是说“嘿,程序员,在初始化块中发生异常”
有关初始化块here
的更多信息因此,处理NPE将解决问题,但遗憾的是我无法访问可能导致此问题的代码。让我知道,我将编辑答案。
答案 1 :(得分:1)
@Vlad Ilie感谢您一看,现在已经解决了..问题在于jar创建ant脚本。 jar创建的早期ant任务无法在类路径中绑定所有jar并导致... classnotfoundexception,这反过来导致ExceptionInInitializerError和NullPointerException。
下面是修复的“jar”任务,它可以成功地添加类路径中的所有jar。
<target name="jar" depends="compile">
<jar destfile="${env.ANT_HOME}/lib/custom-task.jar" basedir="build/classes" >
<zipgroupfileset dir="${basedir}/libs/" includes="*.jar"/>
</jar>
</target>
上面我使用的是zipgroupfileset,非常方便。