18

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
Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

2 Answers2

39

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.

Community
  • 1
  • 1
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • If my package = `com.mycompany.app` and my main class is `com.mycompany.app.MailServerApplication`, `` = latter? – Kevin Meredith Apr 13 '13 at 17:07
  • Yup, the fully qualified class name of a class that has a `public static void main(String[] args)`. – Boris the Spider Apr 13 '13 at 17:10
  • Thanks. Looks like I have my next issue to resolve: `vagrant$ java -jar target/my-app-1.0-SNAPSHOT.jar` Exception in thread "main" java.lang.NoClassDefFoundError: org/restlet/Application – Kevin Meredith Apr 13 '13 at 17:14
  • 1
    Looks like you need to set a classpath or [package into a single file](http://stackoverflow.com/a/574650/2071828). – Boris the Spider Apr 13 '13 at 17:20
4

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
seriousdev
  • 7,519
  • 8
  • 45
  • 52
TheEwook
  • 11,037
  • 6
  • 36
  • 55
  • Hmm: `vagrant$ java -jar target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.MailServerApplication` Failed to load Main-Class manifest attribute from target/my-app-1.0-SNAPSHOT.jar – Kevin Meredith Apr 13 '13 at 17:10
  • Check your MANIFEST.MF in your jar – TheEwook Apr 13 '13 at 17:11
  • 6
    I think you are thinking of `java -cp foo.jar full.package.name.ClassName`. This answer isn't correct. -1 – Boris the Spider Apr 13 '13 at 17:11
  • @bmorris no no, `java -jar foo.jar full.package.name.ClassName` is the command to execute. Maybe `java -cp` work but I have always used `java -jar` – TheEwook Apr 13 '13 at 17:13
  • Take a look at the [docs](http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html) - "If the -jar option is specified, the first non-option argument is the name of a JAR archive containing class and resource files for the application, with the startup class indicated by the Main-Class manifest header." – Boris the Spider Apr 13 '13 at 17:14