0

I'm trying to trigger main class from a jar file. this is the command I'm using:

java -cp Myjar.jar com.example.MainClass 

and I'm getting below error:

could not find or load main class com.example.MainClass

Points to Poinder:

  1. This is a fat jar

  2. I don't have any main class defined in manifest which I think should be fine. (correct me if I'm wrong) So my manifest looks like:

    Manifest 1.0

  3. I'm using the correct path.

  4. I tried to remove the contents of psvm and tried to run with sysout but still it gives same error.

Anaylsis: I could see, anonymous class generated with my main class. So when I grep MainClass in jar file, it gives output as

path_to_file/MainClass
path_to_file/MainClass$1

I also tried to create a jar using intellij i.e. using

project structure -> artifacts -> create jar

then build | build artifacts | build. That also didn't seem to work.

My understanding: Java is able to find the class but seems there is issue with loading it. But I'm unable to figure out why is it unable to load the class. I tried to create a different project's jar and run with same java, it works fine. So there doesn't seem to be issue with java.

Please help me with this, I've tried to search the internet for 2 days now and couldn't find anything helpful. Any help would be highly appreciated.

tez
  • 119
  • 1
  • 1
  • 14
  • You need to specify fully qualified name. Check here - https://stackoverflow.com/a/27109001/2478531 – Pankaj Aug 18 '20 at 14:00
  • yes, I'm specifying full qualified name. Sorry my bad, I missed to mention it. – tez Aug 18 '20 at 14:06
  • What tool are you using to create the fat jar? (Spring boot? Shadow?) Some tools relocates the class files and libraries so they don't conflict with one another inside the jar. You can also try unpackaging the jar file. Your command will only work if there is a folder `com/example` inside it containing the file `MainClass.class`. – Bjørn Vester Aug 18 '20 at 14:59
  • I'm using fatJar. I could figure out, it was due to signed jars. And I'm trying to resolve it. Using CI, I deleted signed jars after fatJar is created, where my log4j jar also got removed. But that's not the best approach I think. So I'm trying to remove signed jars before creating fatJar itself. – tez Aug 19 '20 at 07:05

0 Answers0