简体   繁体   中英

Jar file couldn't load or find main class

I've been trying to run a app from a Jarfile, but it keeps printing out:

"cannot find or load main class ...".

I tried to solve this problem using infos from this thread but all seemed to be useless. To be honest, I'm getting desperate because of the fact that this is such a trivial problem.

Anyways, what I did:

Main-Class: com.test.Test

my manifest attribute:

jar cfm test.jar manifest.txt <full_path>/out/com/test/*.class

which is what's packaged into the jar file (the Test.class file)

The Test class:

package com.test;

public class Test {
  public static void main(String[] args) {
      System.out.println("Hello World");
   }
}

Firstly, just in case you are using Eclipse IDE, there is some tools like the Fat Jar Plugin which are able to help you to package your build.

Secondly, there is Maven, to handle your dependencies, and build the package you need with everything ok. In your case, I will look for the Apache Maven Jar Plugin .

Finally, the old school way to go with the commandline, as you tried to do. As Eliott Frisch has said in your question comments, you don't need to provide the fullpath to the mainclass inside your jar cfm test.jar manifest.txt <full_path>/out/ . And what's because the manifest.txt already give the package information!

You should package the class correctly. Do

jar cfm test.jar manifest.txt com/test/*.class

in the parent folder of the folder com . By providing the absolute path (the way you did), the class file is packaged incorrectly.

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