简体   繁体   中英

Running jar file returns error: Error: Could not find or load main class MyClass Caused by: java.lang.ClassNotFoundException: MyClass

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.

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

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM