Software Development

Why do I get the "no main manifest attribute" error when trying to run a Java JAR file?

T Asked by Thomas Reed · 15-01-2023
0 upvotes 13,702 views 0 comments
The question

I just built my Java project and tried executing it using the command java -jar myapp.jar. However, the terminal returns a "no main manifest attribute" error. I have a main method in my code, so why isn't the JVM finding it? How do I correctly configure my manifest file or my build tool like Maven or Gradle to ensure the Main-Class attribute is included in the final JAR package?

3 answers

0
PA
Answered on 17-01-2023

The "no main manifest attribute" error occurs because your JAR's metadata file, located at META-INF/MANIFEST.MF, lacks the Main-Class entry. Without this, the JVM doesn't know which class contains the entry point for your application. If you are using Maven, you must include the maven-jar-plugin in your pom.xml and specify the <mainClass> inside the <archive><manifest> configuration. For Gradle users, you add a jar task with manifest { attributes 'Main-Class': 'com.yourpackage.Main' }. Once you re-run the package command, the plugin will automatically inject the required line into the manifest. Always verify the output by unzipping the JAR and checking the manifest file to ensure the class name is fully qualified, including the package prefix.

0
ST
Answered on 10-02-2023

Does this error also happen if I'm using an IDE like IntelliJ or Eclipse to export the JAR, or is it strictly a command-line build issue? 

MA 12-02-0023

Steven, it happens in IDEs too if the artifact configuration isn't set up to include the manifest. In IntelliJ, you have to go to Project Structure -> Artifacts and ensure you've selected a Main Class during the "JAR from modules" setup. If you're using Maven/Gradle within the IDE, the IDE will respect the plugin settings in your build file. If those are missing, the exported JAR will still be "empty" of that specific attribute. It's best to rely on your build script rather than the IDE's GUI for consistency across team environments and CI/CD pipelines.

0
NA
Answered on 25-06-2023

You can also fix this on the fly without a build tool. Use the command jar cfe myapp.jar com.example.Main *.class to create a JAR and specify the entry point directly via the -e flag.

TH 27-06-2023

I agree with Nancy. That's a great quick fix for simple projects. I've used that for small utility scripts where I didn't want the overhead of a full Maven setup. It's definitely the fastest way to get an executable JAR running.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session