"找不到主类:MineAvtaler。程序将退出。” - 仅当在 Eclipse 外部运行时
我已经在 Eclipse 中创建了我的应用程序。它相当简单,总共包含大约 1000 行代码。我的类 MineAvtaler
包含我的主要功能。
我通过 Eclipse 的“运行”按钮成功运行了它。但是,每当我尝试从命令行运行代码时,我都会在标题中收到错误消息。我已经编译如下: javac MineAvtaler.java Avtaledata.java Avtale.java
并尝试按如下方式运行应用程序:
- java MineAvtaler
- javaw MineAvtaler.java
后者在消息框中给出错误消息在控制台中。
为什么我的程序不能在 Eclipse 之外运行?
我使用的是 Windows 7 并安装了 JDK。这是我得到的堆栈跟踪:
C:\Users\Codemonkey1991\Desktop\mineavtaler>java MineAvtaler Avtaledata Avtale
Exception in thread "main" java.lang.NoClassDefFoundError: MineAvtaler (wrong name: mineavtaler/MineAvtaler)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
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$000(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)
Could not find the main class: MineAvtaler. Program will exit.
*这是源代码:*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是包的问题。
MineAvtaler
位于名为mineavtaler
的包内(字符大小写相关),如源代码第一行中所述。因此,您可能有一个由 Eclipse 为您创建的名为mineavtaler
的目录。这不是您为项目创建的目录,而是由 Eclipse 创建的“特殊”目录,其中放置您的MineAvtaler.java
文件。您不必在这里打开终端。相反,在父目录中打开它并运行:
javamineavtaler.MineAvtaler
。这样它应该可以工作。编辑:从
C:\Users\Codemonkey1991\Desktop\
运行javamineavtaler.MineAvtaler
当调用“java”命令时,你总是必须观察包结构。请参阅 http://download.oracle.com/javase/tutorial/java/ package/packages.html 了解更多信息。
It's a problem of packages.
MineAvtaler
is inside a package namedmineavtaler
(character capitalization is relevant), like stated in the first line of your source. So you probably have a directory calledmineavtaler
, created for you by Eclipse. This is not the directory you created for the project, is a 'special' directory created by Eclipse in which it put yourMineAvtaler.java
file.You don't have to open a terminal here. Instead, open it on the parent directory and run:
java mineavtaler.MineAvtaler
. This way it should work.EDIT: run
java mineavtaler.MineAvtaler
fromC:\Users\Codemonkey1991\Desktop\
When calling the "java" command you always have to observe the package structure. See http://download.oracle.com/javase/tutorial/java/package/packages.html for more info.