我已经尝试了Oracle的Java教程中的两个示例。它们都可以编译,但在运行时,都会出现这个错误:

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文件放在错误的文件夹里了。

下面是目录层次结构:

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图形运行它。Main(从命令行),它工作正常。

真的很晚更新#2

我没有使用Eclipse(只有notepad++和JDK),上面的更新解决了我的问题。然而,这些答案中似乎有许多是针对Eclipse和IntelliJ IDEA的,但它们具有类似的概念。


当前回答

在我的环境中,我在单元测试中遇到了这个问题。在将一个库依赖项附加到*之后。砰,这是固定的。

例子:

错误信息:

java.lang.NoClassDefFoundError: com/abc/def/foo/xyz/Iottt

POM内容:

<dependency>
    <groupId>com.abc.def</groupId>
    <artifactId>foo</artifactId>
    <scope>test</scope>
</dependency>

其他回答

Java中的NoClassDefFoundError:

定义:

如果一个类在编译时存在,但在运行时java类路径中不可用,则会出现NoClassDefFoundError。通常,当你获得NoClassDefFoundError时,你会在log中看到下面这行: 线程“main”异常

可能的原因:

The class is not available in Java Classpath. You might be running your program using jar command and class was not defined in manifest file's ClassPath attribute. Any start-up script is overriding Classpath environment variable. Because NoClassDefFoundError is a subclass of java.lang.LinkageError it can also come if one of it dependency like native library may not available. Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due to the failure of static initialization is quite common. If you are working in J2EE environment than the visibility of Class among multiple Classloader can also cause java.lang.NoClassDefFoundError, see examples and scenario section for detailed discussion.

可能的决议:

验证所有必需的Java类都包含在应用程序的类路径中。最常见的错误是,在开始执行依赖于一些外部库的Java应用程序之前,没有包括所有必要的类。 应用程序的类路径是正确的,但是classpath环境变量在应用程序执行之前被重写。 验证前面提到的ExceptionInInitializerError没有出现在应用程序的堆栈跟踪中。

资源:

解决Java J2EE中Java .lang. noclassdeffounderror的3种方法

noclassdeffounderror -如何解决没有类Def发现错误

我在Git分支更改后得到了这个错误。对于Eclipse的特定情况,在org.eclipse.wst.common.component文件的.settings目录中遗漏了几行。如下图所示。

使用Maven安装恢复项目依赖关系会有所帮助。

如果你的项目是在一个包,如com。blahcode和你的类被称为Main,编译后的文件可能会以。/out/com/blahcode/Main.class这样的目录结构输出。对于IntelliJ IDEA来说尤其如此。

当试图从shell或cmd运行时,您需要cd到包含com作为子目录的目录。

cd out
java -classpath . com.blahcode.Main

如果您正在使用一个以上的模块,您应该有

dexOptions {
    preDexLibraries = false
}

在构建文件中。

我的观点是:

确保类路径包含完整路径(/home/user/lib/some_lib.jar而不是~/lib/some_lib.jar),否则仍然会面临NoClassDefFoundError错误。