英文:
Running jar file returns error: Error: Could not find or load main class MyClass Caused by: java.lang.ClassNotFoundException: MyClass
问题
我有几个 .class 文件,我想将它们合并为一个 .jar 文件。这是 jar 文件的清单(MANIFEST.MF):
Main-Class: Server
这是我用来编译文件的命令:
jar cfm Server.jar META_INF/MANIFEST.MF ../server/Server.class ../server/message/User.class ../server/message/ServerNode.class ../server/message/ServerHub.class ../server/message/Chattype.class ../server/message/Chat.class
最后,这是我的文件夹结构:
message-platform -
dist -
META-INF -
MANIFEST.MF
server -
Server.class
message -
User.class
ServerNode.class
Chat.class
ServerHub.class
Chattype.class
我得到的错误消息如下:
Error: Could not find or load main class Server
Caused by: java.lang.ClassNotFoundException: Server
这是我执行 .jar 文件的方法:
java -jar Server.jar
在使用 Atom 检查 jar 文件时,出现了这个:
有人能帮助我解决这个问题吗?谢谢!
更新:
我创建了 server 包,现在一切正常。感谢所有尝试帮助过我的人。
英文:
I have a few .class files I'd like to turn into one .jar file. Here is the manifest for the jar file:
MANIFEST.MF:
Main-Class: Server
Here is the command I'm using to compile the file:
jar cfm Server.jar META_INF/MANIFEST.MF ../server/Server.class ../server/message/User.class ../server/message/ServerNode.class ../server/message/ServerHub.class ../server/message/Chattype.class ../server/message/Chat.class
and finally here is my folder structure:
message-platform -
dist -
META-INF -
MANIFEST.MF
server -
Server.class
message -
User.class
ServerNode.class
Chat.class
ServerHub.class
Chattype.class
Here is the error message I'm getting:
Error: Could not find or load main class Server
Caused by: java.lang.ClassNotFoundException: Server
This is how I execute the .jar file:
java -jar Server.jar
When inspecting the jar file using Atom, this appears:
Could someone please help me figure this out? Thanks
UPDATE:
I created the server package and everything is working now. Thanks to everyone who tried to help.
答案1
得分: 2
包名是类名的一部分。server.Server
表示的不是Server
。我认为你想要前者,而不是后者。将
Main-Class: Server
改为
Main-Class: server.Server
英文:
The package name is part of the class name. server.Server
is not Server
. I think you want the former, not the later. Change
Main-Class: Server
to
Main-Class: server.Server
答案2
得分: 2
你曾经说过你的Server.class不属于任何包,如果是这样,你的Server.class就不应该在jar文件中的server文件夹下。如果你希望将server文件夹视为项目结构中的“src”文件夹,可以使用“-C”选项,如下所示:
jar cfm Server.jar manifest.txt -C server Server.class
对于server/message下的所有其他类文件,你应该采取类似的操作。
英文:
You have said that your Server.class does not belong to any package, and if so, your Server.class should not be under server folder in jar file. If you want to treat server folder just as 'src' folder in the project structure, use '-C' option like:
jar cfm Server.jar manifest.txt -C server Server.class
For all other class files under server/message, you should do the similar.
答案3
得分: 0
不确定如何完全修复此问题,但我认为与IDE的类路径配置有关。确保它使用的是标准JDK,并且所有的源代码等都链接正确。也许尝试使用类路径选项 -cp(尽管大多数IDE会自动处理这一点)。例如:
java -cp /home/User/where/my/jar/is -jar example.jar
老实说,我只在进行一些Android开发时遇到过这个问题(它在JVM上运行),所以我只是提出了一些建议。希望你能解决这个问题!
英文:
Not sure how to fix this entirely but I think it'll have something to do with the class path configuration with your IDE. Make sure it's using a standard JDK and that all sources etc are linked up correctly. Perhaps try using the class path option -cp (although most IDEs handle this for you). For example:
java -cp /home/User/where/my/jar/is -jar example.jar
Honestly, I've only really had issues with this when doing some android development (which runs on the JVM itself) so I'm just throwing suggestions. Hope you get it figured out!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论