1

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");
   }
}
Community
  • 1
  • 1
Wecherowski
  • 818
  • 11
  • 24

2 Answers2

1

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!

BendaThierry.com
  • 2,080
  • 1
  • 15
  • 17
1

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.

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
  • Or use `-C` as in http://stackoverflow.com/a/18972208/2868801 . Also when you need only `Main-class` in your manifest, you *can* use `-e` and classname instead of `-m` and manifest file. – dave_thompson_085 Apr 04 '16 at 19:09
  • @dave_thompson_085 that's a fine suggestion, however OP's problem is that the class is not packaged correctly. Simply changing the path of the class (to a relative one) should suffice, everything else remaining the same. – Kedar Mhaswade Apr 04 '16 at 23:38
  • changing the full to relative path didn't work Kedar and @ThierryB – Wecherowski Apr 05 '16 at 16:04
  • @PhilEvening I am surprised it didn't. I tried it out myself before posting this solution and it works beautifully. It's not clear why it does not work for you. Please see [this gist with the series of commands](https://gist.github.com/kedarmhaswade/f18e3d3809c8467ccaa76958fd5b9542). – Kedar Mhaswade Apr 06 '16 at 01:49
  • thanks for your help, in the end I just used the jar-file creating tool of my IDE. Still strange it doesn't workwith cmd though... – Wecherowski Apr 07 '16 at 19:41
  • (Sorry for delay -- taxes.) I agree relative path should fix the problem (and works for me) -- and `-C` is precisely a way to use relative path without cd'ing your shell/CMD to the correct directory. I'm not saying it's better, just an alternative. – dave_thompson_085 Apr 16 '16 at 12:52