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


当前回答

对于我的项目,解决这个问题的是Chrome浏览器和chromedriver不兼容。我有一个非常旧的驱动程序版本,甚至不能打开浏览器。我刚刚下载了两者的最新版本,问题解决了。

我是如何发现这个问题的?因为我使用Selenium本机Firefox驱动程序运行我的项目,我的应用程序中包含一个旧版本的FF。我意识到问题是浏览器和驱动程序不兼容。

希望这可以帮助任何人与我类似的问题,产生了同样的错误消息。

其他回答

对于Meteor或Cordova用户,

这可能是由您使用的Java版本引起的。对于Meteor和Cordova,现在坚持使用版本8。

检查可用的Java版本/usr/libexec/java_home -V,并查找Java版本8的路径名 设置Java版本8的路径 出口JAVA_HOME = /图书馆/ Java / Home / JavaVirtualMachines / jdk1.8.0_191.jdk /内容 检查是否完成 echo $ JAVA_HOME

继续编写代码。

当在类路径中找不到预期的类时,就会发生无类定义异常。

在编译时类:类是从Java编译器生成的,但在运行时不知道为什么找不到依赖类。

让我们来看一个简单的例子:

public class ClassA{
    public static void main(String args[]){
         // Some gibberish code...
         String text = ClassB.getString();
         System.out.println("Text is: " + text);
    }
}

public class ClassB{
    public static String getString(){
        return "Testing some exception";
    }
}

现在让我们假设上述两个Java源代码被放置在某个文件夹中,例如“NoClassDefinationFoundExceptionDemo”

现在打开一个shell(假设Java已经正确设置)

Go to folder "NoClassDefinationFoundExceptionDemo" Compile Java source files javac ClassB javac ClassA Both files are compiled successfully and generated class files in the same folder as ClassA.class and ClassB.class Now since we are overriding ClassPath to the current working director, we execute the following command java -cp . ClassA and it worked successfully and you will see the output on the screen Now let's say, you removed ClassB.class file from the present directory. And now you execute the command again. java -cp . ClassA Now it will greet you with NoClassDefFoundException. As ClassB which is a dependency for ClassA is not found in the classpath (i.e., the present working directory).

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

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

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

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

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

当运行时类装入器装入的类不能访问已由Java rootloader装入的类时,我得到NoClassFoundError。因为不同的类装入器在不同的安全域中(根据Java), JVM不允许在运行时装入器地址空间中解析已经由rootloader装入的类。

使用'java -javaagent:trace .jar [your 'java' ARGUMENTS]'运行程序

它产生显示已加载类的输出,以及加载该类的加载器环境。追踪类为什么不能解析是非常有用的。

// ClassLoaderTracer.java
// From: https://blogs.oracle.com/sundararajan/entry/tracing_class_loading_1_5

import java.lang.instrument.*;
import java.security.*;

// manifest.mf
// Premain-Class: ClassLoadTracer

// jar -cvfm tracer.jar manifest.mf ClassLoaderTracer.class

// java -javaagent:tracer.jar  [...]

public class ClassLoadTracer
{
    public static void premain(String agentArgs, Instrumentation inst)
    {
        final java.io.PrintStream out = System.out;
        inst.addTransformer(new ClassFileTransformer() {
            public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

                String pd = (null == protectionDomain) ? "null" : protectionDomain.getCodeSource().toString();
                out.println(className + " loaded by " + loader + " at " + new java.util.Date() + " in " + pd);

                // dump stack trace of the thread loading class
                Thread.dumpStack();

                // we just want the original .class bytes to be loaded!
                // we are not instrumenting it...
                return null;
            }
        });
    }
}

NoClassDefFoundError意味着类在编译时存在于类路径中,但在运行时不存在于类路径中。

如果您正在使用Eclipse,请确保在.classpath文件中将形状、线点和空间对象作为条目。