"找不到主类:MineAvtaler。程序将退出。” - 仅当在 Eclipse 外部运行时

发布于 2024-11-04 21:01:34 字数 1759 浏览 0 评论 0 原文

我已经在 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.

*这是源代码:*

I've made my application in Eclipse. It's fairly simple and contains ~1000 lines total with code. My class MineAvtaler contains my main function.

I run this successfully from Eclipse's "Run" button. However whenever I try to run the code from command line, I get the error message in the title. I've compiled as such: javac MineAvtaler.java Avtaledata.java Avtale.java

And tried to run the application as such:

  • java MineAvtaler
  • javaw MineAvtaler.java

The latter gives the error message in a message box instead of in the console.

Why won't my program run outside of Eclipse?

I'm using Windows 7 and have JDK installed. This is the stacktrace I get:

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.

*Here is the source code: *

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好多鱼好多余 2024-11-11 21:01:34

是包的问题。 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 named mineavtaler (character capitalization is relevant), like stated in the first line of your source. So you probably have a directory called mineavtaler, 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 your MineAvtaler.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 from C:\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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文