java.lang.NoClassDefFoundError: org/jdom2/Content



我有一个使用jdom的小代码,它创建了一个非常简单的xml,但当我在编译类后尝试运行它时,会在标题java.lang.NoClassDefFoundError: org/jdom2/Content中输出错误

我的jdom.jar文件与java文件位于同一目录中。为了编译类,我使用了javac -cp "jdom.jar" -d . *.java,然后使用java packagename.classname来执行它

这就是代码,尽管我很确定它不需要

package jtest;
import java.io.*;
import org.jdom2.*;
import org.jdom2.input.*;
import org.jdom2.output.*;
public class JDomTest {
public static void main(String[] args) {
try {
Element concesionario = new Element("concesionario");
Document doc = new Document(concesionario);
Element coches = new Element("coches");
Element coche = new Element("coche");
Element matricula = new Element("matricula");
matricula.setText("1111AAA");
Element marca = new Element("marca");
marca.setText("AUDI");
Element precio = new Element("precio");
precio.setText("30000");
coche.addContent(matricula);
coche.addContent(marca);
coche.addContent(precio);
coches.addContent(coche);
coche = new Element("coche");
matricula = new Element("matricula");
matricula.setText("2222BBB");
marca = new Element("marca");
marca.setText("SEAT");
precio = new Element("precio");
precio.setText("10000");
coche.addContent(matricula);
coche.addContent(marca);
coche.addContent(precio);
coches.addContent(coche);
coche = new Element("coche");
matricula = new Element("matricula");
matricula.setText("3333CCC");
marca = new Element("marca");
marca.setText("BMW");
precio = new Element("precio");
precio.setText("20000");
coche.addContent(matricula);
coche.addContent(marca);
coche.addContent(precio);
coches.addContent(coche);
coche = new Element("coche");
matricula = new Element("matricula");
matricula.setText("4444DDD");
marca = new Element("marca");
marca.setText("TOYOTA");
precio = new Element("precio");
precio.setText("10000");
coche.addContent(matricula);
coche.addContent(marca);
coche.addContent(precio);
coches.addContent(coche);
XMLOutputter xml = new XMLOutputter();
xml.setFormat(Format.getPrettyFormat());
xml.output(doc, new FileWriter("text.xml"));
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}

我在Ubuntu 22.04 上使用Java 11

仅使用java packagename.classnamejdom.jar时,将不在类路径中。您必须使用-cp--classpath指定完整的类路径,例如

java -cp "./jdom.jar:./" packagename.classname

注意:在Linux上,分隔符是:,而在Windows上,它是;

最新更新