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
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.
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?
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.
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.
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.