Maven - Can't Execute JAR

Kevin Meredith picture Kevin Meredith · Apr 13, 2013 · Viewed 40.2k times · Source

After building a sample mvn project, I added my org.restlet dependencies & Java code.

Then, I successfully built my JAR via mvn install. Finally, I ran into an error when trying to run the JAR.

vagrant$ java -jar target/my-app-1.0-SNAPSHOT.jar 
Failed to load Main-Class manifest attribute from
target/my-app-1.0-SNAPSHOT.jar

Answer

Boris the Spider picture Boris the Spider · Apr 13, 2013

You need to set the main class in the manifest using the maven-jar-plugin

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.someclass.Main</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

Taken from here.

EDIT

If you want to package the resulting jar with dependencies you can use this

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Taken from here.