从命令提示符运行 jar 时出错
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,
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.
当您的 MANIFEST.MF 文件不正确时,通常会发生这种情况。它位于您的 *.jar 文件中。
我的一个项目的示例:
有关更多信息,请参见此处:
http://java.sun.com/developer/Books/javaprogramming /JAR/basics/manifest.html
http://en.wikipedia.org/wiki/Manifest_file
要更改 MANIFEST.MF 文件应该可以解决您的问题。
编辑:回应“Jon Skeet”:我假设您有一个包含该
方法的类。如果您没有主类 - 忘记我的答案并创建一个主类;-)
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:
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
method. If you don't have a mainclass - forget my answer and create a Main class ;-)