Home » Java » java – Error: Could not find or load main class while executing a .jar file-Exceptionshub

java – Error: Could not find or load main class while executing a .jar file-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have created a Dropwizard application using Intellij.
(Here com.indore.GalaxyApp is name of my MainClass)

This is the pom.xml of my application.

After building the project using mvn clean package, in my target directory a jar file is created.

enter image description here

enter image description here

Now, I am trying to run the application with the jar file through terminal, I get the following response :

enter image description here

enter image description here
MANIFEST.MF

enter image description here

In order to make the jar executable

  1. I have configured the Maven Archiver and added the following plugin in pom.xml acc to this SO answer

    enter image description here

  2. I even run the application by:

    java -cp target/galaxy-1.0-SNAPSHOT.jar com.indore.GalaxyApp

but still getting the same error.

I have referred to these post :

setup-main-class-in-manifest, Cant execute jar file

Can anyone tell me a way to fix this issue??

How to&Answers:

You are trying to run a dropwizard application from command line.

Please follow below steps to create a fat jar and run it from the command line:

  1. Check if your main class FQCN com.indore.GalaxyApp is added in your pom.xml properties tag as <mainClass>com.indore.GalaxyApp</mainClass>
  2. Check Maven jar plugin is present as below:

                <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>${mainClass}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
    
  3. Run your application using java -jar target/galaxy-1.0-SNAPSHOT.jar server <your-config>.yml