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 
2
  • It's above - Failed to load Main-Class manifest attribute from target/my-app-1.0-SNAPSHOT.jar Commented Apr 13, 2013 at 16:47
  • 1
    instead of executing "java -jar", can you place your jar in classpath and try to run something like "java MainClass" Commented Apr 13, 2013 at 16:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

If my package = com.mycompany.app and my main class is com.mycompany.app.MailServerApplication, <mainClass> = latter?
Yup, the fully qualified class name of a class that has a public static void main(String[] args).
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
Looks like you need to set a classpath or package into a single file.
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 

5 Comments

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
Check your MANIFEST.MF in your jar
I think you are thinking of java -cp foo.jar full.package.name.ClassName. This answer isn't correct. -1
@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
Take a look at the docs - "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."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.