Java NoClassDefFoundError 错误
我编写了下面的基本代码并保存到名为 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, DocumentException{
System.out.println("Hello World2!");
}
}
然后我像这样编译它,
javac pdf.java -cp core-renderer.jar:iText-2.0.8.jar
这似乎可以工作,因为我得到了 pdf.class 文件。然后我尝试使用以下命令运行它。
java pdf
我得到以下输出,
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)
我做错了什么?
提前致谢。
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, DocumentException{
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不是说
java pdf.pdf
因为您的pdf
类位于 pdf 包中吗?如果您的 pdf 类位于 pdf 包中,则它应该位于 pdf 目录中(就像您的 MyClass 类位于 my.package 包中一样,它应该位于 my/package/ 目录中)。
要么进入 pdf 目录并使用
-d
选项javac -d 。 pdf
或者进入父目录并执行 javac pdf/pdf.java`Didn't you mean
java pdf.pdf
as yourpdf
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
optionjavac -d . pdf
or you go in the parent directory and do javac pdf/pdf.java`当你编译它时,pdf.class 会在当前目录中生成。将其更改为:
这将在
./pdf
子目录中生成pdf.class
。然后运行如下:When you compiled it so, the pdf.class was generated in current directory. Change it to:
Which will generate
pdf.class
in./pdf
subdirectory. Then run it as follows:首先,由于类是在包中声明的,因此您必须使用包路径存储并调用它。
您需要创建与包结构相匹配的目录结构。在您的情况下,您需要创建一个名为
pdf
的目录并将pdf.class
移入其中。然后,您将使用“java pdf.pdf”进行调用,如科林的回答中指出的那样。
这可能足以运行您当前的示例代码,因为它实际上并没有做太多事情。一旦您开始添加更多功能,您可能需要在执行时将库包含在类路径中,如理查德的回答中所述。
顺便说一句,Java 编程中通常会初始化类名,例如
Pdf
将是示例中的类名,而pdf
将是包名称。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 movepdf.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, whilepdf
would be the package name.编译时的类路径和运行时的类路径是两个不同的东西。但是,它们通常是相同的,因为编译依赖项与运行时依赖项相同。由于您的 main() 方法抛出了 iText-2.0.8.jar 中定义的内容(我认为),因此您应该相应地定义您的类路径:
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 iniText-2.0.8.jar
(I think), you sould define your classpath accordingly: