Building an executable JAR with Netbeans and Maven (no main manifest attribute)

By Steve Claridge on 2016-07-28.

So you've built a JAR with Netbeans, Maven project and get the "no main manifest attribute" error when you try to run it from the command line? This solution relates to Netbeans version 8.0.2 and earlier.

When you run your JAR project from inside the Netbeans IDE you can set the main class from the Run tab of Project Properties, which means the code executes fine from within the IDE. But when you run it from the command line with something like:

java -jar yourjar.jar my.package.Main

you get "no main manifest attribute".

This happens because the project is being built by Maven and the Main Class you specified in the Netbeans properties is not exported by Netbeans to Maven's pom.xml file; in other words: Netbeans knows what your main class is but Maven doesn't.

To fix this you need to edit your project's pom.xml file. You can find this by going to the Projects explorer in Netbeans and click on your project's Project Files folder, once expanded you should see the pom file, double-click it to edit it directly in Netbeans.

You need to add the following XML snippet to your pom.xml, it can go anywhere inside the <project> element, I usually put it after the <properties> element:

                            <mainClass>MAIN CLASS GOES HERE</mainClass>

Just replace MAIN CLASS GOES HERE with your main class, rebuild and your JAR is now runnable. If you are unsure what to put here, just copy the Main Class settings from the Rub tab in your Netbeans Project Properties.