Search code examples
javaspringspring-bootintellij-idea

Java Could not find or load main class when running jar file


I have a springboot application, it runs just fine in intellij. I'm trying to create an executable jar to run the application on other machines. I followed this

tutorialhttps://www.jetbrains.com/idea/guide/tutorials/hello-world/packaging-the-application/

down to every step, and managed to build my jar. but when i try to run the jar with

java -jar nameofjar.java

I get a Could not find or load main class error. I unzipped the jar file with java c and the classpath correctly points to the main class, so im really lost as to what i have to fix here to get java to detect the main class.

I've tried a bunch of solutions from here:

Error: Could not find or load main class in intelliJ IDE

such as deleting .idea or Rebuild Project or doing mvn clean package but nothing seems to work.

Using java -c to open the jar file and verify that the main classpath is indeed correct enter image description here

Main class and file structure: main class

This run config works perfectly fine in intellij enter image description here

here is my manifest.mf:

Manifest-Version: 1.0
Main-Class: com.owl.PosApi.OwlPosApiApplication

Solution

  • If you build jar artifact using IntelliJ IDEA, it may be incomplete or manifests from the other included dependencies can override your custom one with the main class defined.

    Related answers:

    The proper solution would be to build the artifact using Maven. It may work out of the box with mvn clean package or you may need to modify pom.xml to build the fat jar with all the dependencies per this document.