如何解决“java.lang.NoClassDefFoundError"? [英] How can I solve "java.lang.NoClassDefFoundError"?

查看:42
本文介绍了如何解决“java.lang.NoClassDefFoundError"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了 Oracle Java 教程中的两个示例.他们都编译得很好,但在运行时,都出现了这个错误:

I've tried both the examples 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.

这是目录层次结构:

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类放入graphics包后(我添加了package graphics;),将类路径设置为"_测试"(包含图形的文件夹),编译它,并使用 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 IDEA,但它们具有相似的概念.

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 IDEA, 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 语言惊人的工具.Eclipse 和 IntelliJ IDEA 等现代 IDE 以及 Maven 或 Gradle 等构建管理工具将帮助您不必担心类路径(尽可能多)并专注于代码!也就是说,此链接 解释了在执行命令时如何设置类路径线.

Side note: I would recommend taking advantage of the amazing tooling that exists for Java and JVM languages. Modern IDEs like Eclipse and IntelliJ 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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆