简体   繁体   English

类声明错误(由ClassNotFoundException引起的NoClassDefFoundError)

[英]Class declaration bug (NoClassDefFoundError caused by ClassNotFoundException)

Please advise me what's wrong with this class declaration: 请告诉我这个班级宣言有什么问题:

ExchEngine.java ExchEngine.java

package engine;

public class ExchEngine {

    public ExchEngine() {
    }

    public static void main(String[] args) {

        ExchEngine engine = new ExchEngine();

    }
}

When I compile this file, I always get exception: 当我编译这个文件时,我总是得到异常:

java.lang.NoClassDefFoundError: test_engine/ExchEngine
Caused by: java.lang.ClassNotFoundException: test_engine.ExchEngine
 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Exception in thread "main"

This seems very weird that ExchEngine.java is inside a package and it cannot run itself. ExchEngine.java在包中并且无法自行运行,这似乎很奇怪。 Thanks for any help. 谢谢你的帮助。

You should put your source file in a directory called "engine", since that's the package name of gave it. 您应该将源文件放在名为“engine”的目录中,因为这是给它的包名。

Compile the file with javac engine/ExchEngine.java and run it with java engine.ExchEngine . 使用javac engine/ExchEngine.java编译该文件,并使用java engine.ExchEngine运行它。

Your class is engine.ExchEngine . 你的班级是engine.ExchEngine Something somewhere is looking for test_engine.ExchEngine , and is causing this error when it fails to do so. 某处某处正在寻找test_engine.ExchEngine ,并且当它无法执行此操作时会导致此错误。 You presumably need to change the two so that they match. 你可能需要更改这两个以便它们匹配。

This isn't a compile time problem, it is a run time error. 这不是编译时问题,而是运行时错误。

Need more information. 需要更多信息。 For instance, how are you trying to run it? 例如,你是如何尝试运行它的? In Eclipse? 在Eclipse中?

If in Eclipse: You may need to clean the project. 如果在Eclipse中:您可能需要清理项目。 Or you might be trying an old run configuration from before you changed the package. 或者您可能在更改包之前尝试旧的运行配置。

Specify the classpath when running your application 运行应用程序时指定类路径

Assuming after compiling, you have the following directory structure: 假设编译后,您具有以下目录结构:

./test_engine/ExchEngine.class

then run it like so: 然后像这样运行它:

java -cp . test_engine.ExchEngine

If your using an IDE, reconfigure the project, mainly where it looks for the Main method. 如果您使用IDE,请重新配置项目,主要是查找Main方法的位置。 Clean, rebuild, restart, do what ever you need to do. 清洁,重建,重启,做你需要做的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoClassDefFoundError:…由ClassNotFoundException引起 - NoClassDefFoundError: … Caused by ClassNotFoundException loadClass抛出由ClassNotFoundException引起的NoClassDefFoundError - loadClass throws NoClassDefFoundError caused by ClassNotFoundException 如何解决ClassNotFoundException引起的NoClassDefFoundError? - How to solve a NoClassDefFoundError caused by ClassNotFoundException? scala 2.12 NoClassDefFoundError: scala/Product$class 原因:ClassNotFoundException: scala.Product$class - scala 2.12 NoClassDefFoundError: scala/Product$class Caused by: ClassNotFoundException: scala.Product$class 难道NoClassDefFoundError总是有ClassNotFoundException引起的吗? - Does NoClassDefFoundError have always ClassNotFoundException in caused by? 由java.lang.NoClassDefFoundError和ClassNotFoundException引起的错误 - Error caused by java.lang.NoClassDefFoundError and ClassNotFoundException ClassNotFoundException 导致的线程“主”java.lang.NoClassDefFoundError 中的异常 - Exception in thread "main" java.lang.NoClassDefFoundError caused by ClassNotFoundException 将 Java Web 应用程序部署到 Heroku:由 ClassNotFoundException 引起的 NoClassDefFoundError - deploying java web app to heroku: NoClassDefFoundError caused by ClassNotFoundException NoClassDefFoundError原因:java.lang.ClassNotFoundException:junit.textui.ResultPrinter - NoClassDefFoundError Caused by: java.lang.ClassNotFoundException: junit.textui.ResultPrinter 在Hive驱动程序连接线上的Hadoop中由ClassNotFoundException引起的NoClassDefFoundError? - NoClassDefFoundError caused by ClassNotFoundException in Hadoop on Hive Driver Connection line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM