Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Can't Execute JAR

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
like image 434
Kevin Meredith Avatar asked Apr 13 '13 16:04

Kevin Meredith


People also ask

Where do I put manifest file in JAR?

The manifest file is named MANIFEST. MF and is located under the META-INF directory in the JAR. It's simply a list of key and value pairs, called headers or attributes, grouped into sections.


2 Answers

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.

like image 131
Boris the Spider Avatar answered Oct 13 '22 19:10

Boris the Spider


If you dont have a manifest in your jar invoking java -jar will not work.

Use this command if you dont have a manifest:

java -cp foo.jar full.package.name.ClassName
like image 31
TheEwook Avatar answered Oct 13 '22 21:10

TheEwook