我正在 ant 中创建一个自定义任务,在创建该任务时,我面临着 ExceptionInInitializer 异常。我已将 jdom-2.0.2.jar 放在我的 ant lib 目录中,以便成功编译自定义任务。下面是我的自定义任务代码
import java.io.*;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class Demo extends Task {
private String init;
@Override
public void execute() throws BuildException {
if(!init.isEmpty() && init!=null){
display();
}else{
throw new BuildException("Please enter attribute init");
}
}
public void setInit(String init){
this.init=init;
}
public void display(){
System.out.println("init value is "+init);
try {
SAXBuilder saxBuilder = new SAXBuilder();
} catch (Exception e) {
e.printStackTrace();
throw new BuildException("JDOMException caught");
}
}
}
这是我的蚂蚁文件
<project name="XMLUpdate" default="main" basedir=".">
<target name="main" depends="compile,jar,runtask">
</target>
<target name="compile">
<javac srcdir="." destdir="."/>
</target>
<target name="jar">
<jar destfile="./Demo.jar" basedir="." includes="*.class"/>
</target>
<target name="runtask">
<taskdef name="dem" classname="Demo"/>
<dem init="BOM.xml" />
</target>
</project>
我正面临 ExceptionInInitializer 异常
我认为编译器解析行时出现错误
SAXBuilder saxBuilder = new SAXBuilder();
老实说,到目前为止,我还没有在 java 中遇到过这个异常。这里有什么问题?