我已经尝试了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的,但它们具有类似的概念。


当前回答

在编译代码之后,程序中的每个类都有.class文件。这些二进制文件是Java解释以执行程序的字节码。NoClassDefFoundError表示负责动态加载类的类加载器(在本例中为java.net.URLClassLoader)无法为您试图使用的类找到.class文件。

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/。

旁注:我建议充分利用Java和JVM语言的惊人工具。像Eclipse和IntelliJ IDEA这样的现代ide和像Maven或Gradle这样的构建管理工具将帮助您不必担心类路径,而将注意力集中在代码上!也就是说,这个链接解释了在命令行上执行时如何设置类路径。

其他回答

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

例子:

错误信息:

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

POM内容:

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

如果在编译和运行时遇到以下错误之一:

NoClassDefFoundError Error: Could not find or load main class hello Exception in thread "main" java.lang.NoClassDefFoundError:javaTest/test/hello (wrong name: test/hello) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

-------------------------- 解决方案 -----------------------

问题主要出在包装组织上。您应该根据源代码中的包分类在文件夹中适当地安排类。

在编译过程中,使用这个命令:

javac -d . [FileName.java]

要运行类,请使用以下命令:

java [Package].[ClassName]

这个答案特定于服务中发生的java.lang.NoClassDefFoundError:

我的团队最近在升级提供服务的rpm后看到了这个错误。rpm和其中的软件都是用Maven构建的,所以我们似乎有一个编译时依赖项,只是没有包含在rpm中。

然而,在进行调查时,未找到的类与堆栈跟踪中的几个类位于同一个模块中。此外,这不是一个最近才添加到构建中的模块。这些事实表明,这可能不是Maven依赖关系的问题。

最终的解决方案:重新启动服务!

rpm升级似乎使服务在底层JAR文件上的文件句柄失效。然后,该服务看到一个没有加载到内存中的类,在jar文件句柄列表中搜索它,但未能找到它,因为它可以从中加载类的文件句柄已失效。重新启动该服务将迫使它重新加载所有的文件句柄,从而允许它在rpm升级后立即加载内存中没有找到的类。

我在使用Android studio进行Android开发时也遇到了同样的问题。提供的解决方案是一般的,对我没有帮助(至少对我来说)。

经过几个小时的研究,我找到了以下解决方案,它可能会帮助使用Android Studio进行开发的Android开发人员。

修改如下:

首选项→构建、执行、部署→立即运行→*取消第一个选项。

有了这个改变,我就可以开始工作了。

检查类中是否有静态处理程序。如果是这样,请小心,因为静态处理器只能在有循环程序的线程中启动,崩溃可能会以这种方式触发:

首先,在一个简单的线程中创建类的实例并捕获崩溃。 然后在主线程中调用Class的field方法,你会得到NoClassDefFoundError。

下面是测试代码:

public class MyClass{
       private static  Handler mHandler = new Handler();
       public static int num = 0;
}

在Main活动的onCreate方法中,添加测试代码部分:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //test code start
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                MyClass myClass = new MyClass();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }).start();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    MyClass.num = 3;
    // end of test code
}

有一个简单的方法来修复它使用一个handlerThread到init处理程序:

private static Handler mHandler;
private static HandlerThread handlerThread = new HandlerThread("newthread");
static {
    handlerThread.start();
    mHandler = new Handler(handlerThread.getLooper(), mHandlerCB);
}