JAR主类未找到问题
我必须创建一个 JAR 文件(不使用任何 IDE)
这就是我正在做的事情:
我有一个名为 Project 的文件夹,这是主要的文件夹,其中有两个文件夹和一个 pdf 文件。
一个名为 ClassFiles ,包含我的项目的所有“.class”文件,
另一个名为 JavaFiles ,包含所有“.java”文件。
该文件是我必须包含的关系,但没有任何相关内容,并且称为 Info.pdf。
我的错误是这样的:
我的主类(是多余的,我的意思是起始类,带有“public static void main(String[] args)”)被称为Main,
我的命令行使用的是:
jar -cfe Project.jar ClassFiles.Main JavaFiles ClassFiles INFO.pdf
我收到错误 无法找到主类:ClassFiles.Main。程序将退出
PS:在ClassFiles文件夹中我有很多classname$1,classname$2文件。我不知道是否相关。我什至有 Main 和另一个 Main$1。
我明天需要提供这个 JAR,所以我非常绝望。
先感谢您!
Exception in thread "main" java.lang.NoClassDefFoundError: ClassFiles/Main (wrong name: Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: ClassFiles.Main. Program will exit
@埃里克森: ClassFiles 只是我用来包含所有 .class 的文件夹,仅此而已...我所做的事情中没有包(这是我使用 Eclipse 时的默认包)
@Dave Newton:对不起,伙计,(首先感谢你),你能说得更具体一点吗?我不明白我应该做什么。我不太擅长这个东西
啊,如果你的意思是这个包不叫Project,而是Progetto,只是因为我把它翻译成了英文。不确定这是否也是您所指的。
我在 ClassFiles 目录中有 Main.class,在 JavaFiles 目录中有 Main.java
这正是我所做的和结果:
ascal@AscaL ~/Desktop/Progetto $ jar -cfe Progetto.jar ClassFiles.Main ClassFiles JavaFiles Info.pdf
ascal@AscaL ~/Desktop/Progetto $ java -jar Progetto.jar
线程“main”中出现异常 java.lang.NoClassDefFoundError: ClassFiles/Main(错误名称:Main) 在 java.lang.ClassLoader.defineClass1(本机方法) 在 java.lang.ClassLoader.defineClass(ClassLoader.java:634) 在 java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 在 java.net.URLClassLoader.defineClass(URLClassLoader.java:277) 在 java.net.URLClassLoader.access$000(URLClassLoader.java:73) 在 java.net.URLClassLoader$1.run(URLClassLoader.java:212) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.net.URLClassLoader.findClass(URLClassLoader.java:205) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:321) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:266)
找不到主类:ClassFiles.Main。程序将退出。
//Main.java代码
I have to create a JAR file (without using any IDE)
That's what I'm doing:
I got a folder named Project, this is the main one, in this one i two folders and a pdf file.
one named ClassFiles that contains all the ".class" files of my project
the other one named JavaFiles that contains all the ".java" files.
the file is a relation that i have to include, nothing relevant though, and is called Info.pdf.
The error i have is this:
My main class (is redundant, i mean the starting one, with "public static void main(String[] args)") is called Main
the command line i'm using is:
jar -cfe Project.jar ClassFiles.Main JavaFiles ClassFiles INFO.pdf
i get the error Could not find the main class: ClassFiles.Main. Program will exit
PS: in the ClassFiles folder i have a lot of classname$1, classname$2 files. i don't know if it's relevant. I even have the Main with and another Main$1.
I need to give this JAR tomorrow, so i'm quite desperate.
Thank you in advance!
Exception in thread "main" java.lang.NoClassDefFoundError: ClassFiles/Main (wrong name: Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: ClassFiles.Main. Program will exit
@erikson:
ClassFiles is just a folder i made to contain all the .class, nothing more... there is no package in what i did (it was the default one when i used Eclipse)
@ Dave Newton: sorry man, (first of all thank you), could u be a little more specific? i'm not understanding what i should do. i'm not really good with this stuff
ah, if you mean that the package is not called Project but Progetto is just becouse i translated it in english. not sure if that's what you are referring too.
I have the Main.class in the ClassFiles directory and Main.java in the JavaFiles dir
That's exactly what i did and the result:
ascal@AscaL ~/Desktop/Progetto $ jar -cfe Progetto.jar ClassFiles.Main ClassFiles JavaFiles Info.pdf
ascal@AscaL ~/Desktop/Progetto $ java -jar Progetto.jar
Exception in thread "main" java.lang.NoClassDefFoundError: ClassFiles/Main (wrong name: Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: ClassFiles.Main. Program will exit.
//Code of Main.java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您运行
jar
命令时出现该错误吗?对我来说效果很好。当然,如果您通过 java -jar Project.jar 运行它,并且 Main 类不在 ClassFiles 包中,那么它就会崩溃。 Main类在哪个包中?
You get that error when you run the
jar
command? Works fine for me.Of course, if you run it via
java -jar Project.jar
, and the Main class isn't in the ClassFiles package, it'll break. What package is the Main class in?在基本文件夹中创建一个清单文件,其中包含:
然后运行:
希望它有帮助:)
Create a manifest file in the base folder containing:
Then run:
Hope it helps :)
要运行 jar 文件,您应该使用 Java Sun 中给出的规定说明开发基础网站。
To run a jar file, you should use the prescribed directions given in the Java Sun Developing Basics website.