问题描述
我在Oracle的中尝试了这两个示例。它们都编译正常,但在运行时,都出现了这个错误:
I've tried both the example in Oracle's Java Tutorials. They both compile fine, but at run-time, both come up with this error:
Exception in thread "main" java.lang.NoClassDefFoundError: graphics/shapes/Square
at Main.main(Main.java:7)
Caused by: java.lang.ClassNotFoundException: graphics.shapes.Square
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
我想我可能在错误的文件夹中有 Main.java
文件。这是目录层次结构:
I think I might have the Main.java
file in the wrong folder. Here is the directory hierarchy:
graphics
├ Main.java
├ shapes
| ├ Square.java
| ├ Triangle.java
├ linepoint
| ├ Line.java
| ├ Point.java
├ spaceobjects
| ├ Cube.java
| ├ RectPrism.java
这里是 Main.java
:
import graphics.shapes.*;
import graphics.linepoint.*
import graphics.spaceobjects.*;
public class Main {
public static void main(String args[]) {
Square s = new Square(2,3,15);
Line l = new Line(1,5,2,3);
Cube c = new Cube(13,32,22);
}
}
我在这里做错了什么?
更新
我把 Main 类进入
图形
包(我添加包图形;
),将类路径设置为_test (包含图形的文件夹),编译它,并使用 java graphics.Main
(从命令行)运行它,它工作。
After I put put the
Main
class into the graphics
package (I added package graphics;
to it), set the classpath to "_test" (folder containing graphics), compiled it, and ran it using java graphics.Main
(from the command line), it worked.
真的很晚更新#2
我没有使用Eclipse(只是Notepad ++和JDK),以上更新解决了我的问题。但是,似乎许多这些答案都是针对Eclipse和IntelliJ的,但它们有类似的概念。
I wasn't using Eclipse (just Notepad++ and the JDK), and the above update solved my problem. However, it seems that many of these answers are for Eclipse and IntelliJ, but they have similar concepts.
推荐答案
编译完成后代码,您最终得到程序中每个类的
.class
文件。这些二进制文件是Java解释为执行程序的字节码。 NoClassDefFoundError
表示类加载器(在本例中为 java.net.URLClassLoader
),它负责动态加载类,找不到您尝试使用的类的 .class
文件。
After you compile your code, you end up with
.class
files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError
indicates that the classloader (in this case java.net.URLClassLoader
), which is responsible for dynamically loading classes, cannot find the .class
file for the class that you're trying to use.
如果所需的类不存在,您的代码将无法编译(除非使用反射加载类),因此通常此异常意味着您的类路径不包括所需的课程。请记住,类加载器(特别是
java.net.URLClassLoader
)将在类路径的每个条目中的文件夹a / b / c /中查找包a.b.c中的类。 NoClassDefFoundError
还可以表明您错过了您编译过的.jar文件的传递依赖项,并且您正在尝试使用。
Your code wouldn't compile if the required classes weren't present (unless classes are loaded with reflection), so usually this exception means that your classpath doesn't include the required classes. Remember that the classloader (specifically
java.net.URLClassLoader
) will look for classes in package a.b.c in folder a/b/c/ in each entry in your classpath. NoClassDefFoundError
can also indicate that you're missing a transitive dependency of a .jar file that you've compiled against and you're trying to use.
例如,如果您有一个类
com.example.Foo
,那么在编译之后您将拥有一个类文件让Foo.class
。比如说你的工作目录是 ... / project /
。该类文件必须放在 ... / project / com / example
中,并且您将类路径设置为 ... / project /
。
For example, if you had a class
com.example.Foo
, after compiling you would have a class file Foo.class
. Say for example your working directory is .../project/
. That class file must be placed in .../project/com/example
, and you would set your classpath to .../project/
.
附注:我建议利用Java和JVM语言中存在的神奇工具。现代IDE就像Eclipse和IDEA以及构建管理工具(如Maven或Gradle)将帮助您不必担心类路径(同样多)并专注于代码!也就是说,说明了在执行命令时如何设置类路径line。
Side note: I would recommend taking advantage of the amazing tooling that exists for Java and JVM languages. Modern IDE's like Eclipse and IDEA and build management tools like Maven or Gradle will help you not have to worry about classpaths (as much) and focus on the code! That said, this link explains how to set the classpath when you execute on the command line.
这篇关于如何解决java.lang.NoClassDefFoundError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!