Home > Blockchain >  java.lang.NoClassDefFoundError: org/jdom2/Content
java.lang.NoClassDefFoundError: org/jdom2/Content

Time:10-21

I have a small code using jdom that creates a very simple xml, but when I try to run it after compiling the classes, outputs the error in the title java.lang.NoClassDefFoundError: org/jdom2/Content

I have the jdom.jar file in the same directory as the java file. To compile the classes I used javac -cp "jdom.jar" -d . *.java and then java packagename.classname to execute it.

This is the code, even tho I'm pretty sure it is not needed

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());
        }
    }
}

I'm using Java 11 on Ubuntu 22.04

CodePudding user response:

When using just java packagename.classname jdom.jar will not be in your classpath. You will have to specify your full classpath using -cp or --classpath, e.g.

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

Note: on Linux the separator is : while on Windows it is ;

  • Related