dom4j NoClassDefFoundError

  • Thread starter michael.miceli88
  • Start date
M

michael.miceli88

I have a simple application that I can't seem to get to run
correctly. The code is below:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Document;
import org.dom4j.io_OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Foo {
public Document createDocument() {
Document document = DocumentHelper.createDocument();
Element root = document.addElement( "root" );

Element author1 = root.addElement( "author" )
.addAttribute( "name", "James" )
.addAttribute( "location", "UK" )
.addText( "James Strachan" );

Element author2 = root.addElement( "author" )
.addAttribute( "name", "Bob" )
.addAttribute( "location", "US" )
.addText( "Bob McWhirter" );

return document;
}
public static void main(String[] args){
Foo ex = new Foo();
try{
FileWriter out = new FileWriter( "foo.xml" );
Document document;
document = ex.createDocument();
document.write( out );
}
catch(IOException e){
System.out.println("Unable to create file:" + e.getMessage());
}
}
}

and I also have dom4j-1.6.1.jar in the same directory
I compiled the app with javac Foo.java -classpath dom4j-1.6.1.jar and
got no errors, but when I run the code I
get this error:
Exception in thread "main"
java.lang.NoClassDefFoundError: org/dom4j/DocumentHelper
at Foo.createDocument(Foo.java:12)
at Foo.main(Foo.java:32)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentHelper
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:
319)
... 2 more

What am I doing wrong. If I unjar the dom4j-1.6.1.jar the
class labeled DocumentHelper is there.
Thanks,
Michael
 
A

Arne Vajhøj

I have a simple application that I can't seem to get to run
correctly. The code is below:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Document;
import org.dom4j.io_OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Foo {
public Document createDocument() {
Document document = DocumentHelper.createDocument();
Element root = document.addElement( "root" );

Element author1 = root.addElement( "author" )
.addAttribute( "name", "James" )
.addAttribute( "location", "UK" )
.addText( "James Strachan" );

Element author2 = root.addElement( "author" )
.addAttribute( "name", "Bob" )
.addAttribute( "location", "US" )
.addText( "Bob McWhirter" );

return document;
}
public static void main(String[] args){
Foo ex = new Foo();
try{
FileWriter out = new FileWriter( "foo.xml" );
Document document;
document = ex.createDocument();
document.write( out );
}
catch(IOException e){
System.out.println("Unable to create file:" + e.getMessage());
}
}
}

and I also have dom4j-1.6.1.jar in the same directory
I compiled the app with javac Foo.java -classpath dom4j-1.6.1.jar and
got no errors, but when I run the code I
get this error:
Exception in thread "main"
java.lang.NoClassDefFoundError: org/dom4j/DocumentHelper
at Foo.createDocument(Foo.java:12)
at Foo.main(Foo.java:32)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentHelper
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:
319)
... 2 more

What am I doing wrong. If I unjar the dom4j-1.6.1.jar the
class labeled DocumentHelper is there.

How do you run the program ?

It looks as if you forgot to add the dom4j jar to classpath when
running the program.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top