Java NoClassDefFoundError Error
I wrote the basic code below and saved to a file called pdf.java.
package pdf;
import java.util.*;
import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.util.regex.*;
public class pdf {
public static void main(String[] args) throws IOException, DocumentExcepti开发者_如何学运维on{
System.out.println("Hello World2!");
}
}
I then compiled it like this,
javac pdf.java -cp core-renderer.jar:iText-2.0.8.jar
Which seemed to work as I got a pdf.class file. I then tried to run it with the following command.
java pdf
And I got the following output,
Exception in thread "main" java.lang.NoClassDefFoundError: pdf (wrong name: pdf/pdf)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
What am I doing wrong?
Thanks in advance.
Didn't you mean java pdf.pdf
as your pdf
class is in the pdf package ?
If your pdf class is in the pdf package it should be in a pdf directory (as in if your MyClass class is in my.package package, it should be in my/package/ directory).
Either you go into the pdf directory and use the -d
option javac -d . pdf
or you go in the parent directory and do javac pdf/pdf.java`
When you compiled it so, the pdf.class was generated in current directory. Change it to:
javac -cp core-renderer.jar:iText-2.0.8.jar -d . pdf.java
Which will generate pdf.class
in ./pdf
subdirectory. Then run it as follows:
java -cp .:core-renderer.jar:iText-2.0.8.jar -d . pdf.java
First, since the class is declared as in a package, you have to store it and call it using the package path.
You need to create a directory structure that matches the package structure. In your case, you need to create a directory called pdf
and move pdf.class
into it.
Then you would call with `java pdf.pdf' as pointed out in Colin's answer.
This might be sufficient to run your current sample code, since it doesn't really do much of anything. Once you get to adding more functionality, you will likely need to include your libraries on the classpath when executing, as described in Richard's answer.
BTW, it is conventional in Java programming to initcap class names, e.g. Pdf
would be the class name in your example, while pdf
would be the package name.
Classpath for compile-time and for run-time are two different things. However, they are often identical since compilation dependencies are identical as runtime dependencies. Since your main()
method throws something defined in iText-2.0.8.jar
(I think), you sould define your classpath accordingly:
java -cp .:iText-2.0.8.jar pdf.pdf
精彩评论