[英]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.