The right way to Make a Java Jar File Executable


Let’s say you’ve gotten a Java mission as follows:

bundle ms.ao.one thing;
public class MyProject {
    public static void predominant(String...args) throws Exception {
        System.out.println("Howdy world!");
    }
}

Now you need to construct this and make it a self contained executable Jar.

For those who do a mvn clear set up or mvn clear bundle, and try to run it as follows:

java -jar goal/my-java-1.0-SNAPSHOT.jar

You’re going to get the next error:

no predominant manifest attribute, in goal/my-java-1.0-SNAPSHOT.jar

The right way to Remedy no predominant manifest attribute error

You may clear up this error by including the next in your pom.xml file:

<construct>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <model>3.1.1</model>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>ms.ao.one thing.MyProject</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</construct>

Now construct your answer like this:

mvn clear set up bundle

Then run your standalone Jar as follows:

java -jar goal/my-java-1.0-SNAPSHOT.jar

Leave a Reply