从命令提示符运行 jar 时出错

发布于 2024-10-18 03:58:00 字数 781 浏览 1 评论 0原文

我正在使用 NetBeans IDE 来编写 java 程序。当我从 NetBeans 运行该程序时,它工作正常,但是当我从命令提示符运行它时,我得到以下信息:

Exception in thread "main" java.lang.NoClassDefFoundError: javaapplication/Main
Caused by: java.lang.ClassNotFoundException: javaapplication.Main
        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: javaapplication.Main. Program will exit.

我使用另一个程序 (myEclipse) 来构建该程序,但确实发生了这种情况,我只能从以下位置运行该程序IDE 而不是命令提示符。

I'm using NetBeans IDE to code a java program. When I run the program from NetBeans it works perfectly but when I run it from command prompt I get the following:

Exception in thread "main" java.lang.NoClassDefFoundError: javaapplication/Main
Caused by: java.lang.ClassNotFoundException: javaapplication.Main
        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: javaapplication.Main. Program will exit.

I used another program (myEclipse) to build the program but the exact thing happened, I'm only able to run the program from the IDE and not command prompt.

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

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

发布评论

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

评论(2

小情绪 2024-10-25 03:58:00

那么,javaapplication.Main 是否意味着主类?如果不是,则可能是清单文件中的错误。

如果它是主类,请查看 jar 文件内部(例如,通过提取它)。查找一个名为 javaapplication 的目录,其中应该有一个名为 Main.class 的文件。

这两件事中有一个是错误的,但我们无法真正从这里看出它是什么 - 只有您可以。

Well, is javaapplication.Main meant to be the main class? If not, it's probably a mistake in the manifest file.

If it is meant to be the main class, look inside the jar file (e.g. by extracting it). Look for a directory called javaapplication which should have a file called Main.class in.

One of those two things is wrong, but we can't really tell what it is from here - only you can.

后来的我们 2024-10-25 03:58:00

当您的 MANIFEST.MF 文件不正确时,通常会发生这种情况。它位于您的 *.jar 文件中。

我的一个项目的示例:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.2-b01 (Sun Microsystems Inc.)
Class-Path: lib/log4j-1.2.14.jar lib/mysql-connector-java-5.1.11-bin.jar
Main-Class: at.package.to.main.class.MainClass

有关更多信息,请参见此处:
http://java.sun.com/developer/Books/javaprogramming /JAR/basics/manifest.html
http://en.wikipedia.org/wiki/Manifest_file

要更改 MANIFEST.MF 文件应该可以解决您的问题。

编辑:回应“Jon Skeet”:我假设您有一个包含该

public static void main(String args[]){ ... }

方法的类。如果您没有主类 - 忘记我的答案并创建一个主类;-)

This normally happens when your MANIFEST.MF file is not correct. It is located in your *.jar file.

An example from one of my projects:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.2-b01 (Sun Microsystems Inc.)
Class-Path: lib/log4j-1.2.14.jar lib/mysql-connector-java-5.1.11-bin.jar
Main-Class: at.package.to.main.class.MainClass

For more Information see here:
http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html
http://en.wikipedia.org/wiki/Manifest_file

To change your MANIFEST.MF file should fix your problem.

EDIT: in response to "Jon Skeet": I'm assuming that you have a class with the

public static void main(String args[]){ ... }

method. If you don't have a mainclass - forget my answer and create a Main class ;-)

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