Failed to load Main-Class manifest attribute while running java -jar

JavaSpringMavenSpring Mvc

Java Problem Overview


I have successfully built my Spring MVC project with mvn clean package by following this tutorial.

Now I am trying to run the service with:

mvn clean package && java -jar target/gs-serving-web-content-0.1.0.jar

But I get this error:

Failed to load Main-Class manifest attribute from target/gs-serving-web-content-0.1.0.jar

Am I missing something?

Java Solutions


Solution 1 - Java

If you are working with Spring Boot this will solve your problem:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.2.5.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Reference Guide | Spring Boot Maven Plugin

Solution 2 - Java

You might be missing Spring Boot Maven Plugin.

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

Solution 3 - Java

You have to specifiy it in your pom.xml - This will make your jar executable with all dependencies (replace your.main.class):

<!-- setup jar manifest to executable with dependencies -->
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass>your.main.class</mainClass>
      </manifest>
    </archive>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
       <goal>single</goal>
      </goals>  
    </execution>
  </executions>
</plugin>

Solution 4 - Java

check the order of

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

it should be above

dockerfile-maven-plugin else repackage will happen

this solved my problem of no main attribute in manifest.

Solution 5 - Java

You are missing maven-jar-plugin in which you need to add manifest tag.

Solution 6 - Java

For spring boot, I've created a MANIFEST.MF file inside META-INF folder.

in my STS IDE, I placed the META-INFO folder inside the src/main/resources folder like so:

screenshot from STS IDE (eclipse project)

the content of the MANIFEST.MF file:

Manifest-Version: 1.0
Implementation-Title: bankim
Implementation-Version: 1.5.6.RELEASE
Archiver-Version: Plexus Archiver
Built-By: Yourname
Implementation-Vendor-Id: com.bankim
Spring-Boot-Version: 1.5.6.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.bankim.BankimApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Implementation-URL: http://projects.spring.io/spring-boot/bankim/
  1. Every mention of "bankim"/"Bankim" refers to my project name, so replace it with your project name that
  2. take special note of the "Start-Class" value. it should contain the "path" to the class that has your main method.
  3. the row: Main-Class: org.springframework.boot.loader.JarLaunchershould be left as-is.

****the above manifest was created for me by using the "spring-boot-maven-plugin" mentioned above by "Mradul Pandey" (answered Sep 2 '15 at 4:50)

Hope this helps

Solution 7 - Java

java -jar target/gs-serving-web-content-0.1.0.jar command assumes the provided jar is executable. In an executable jar, the main method is defined in MANIFEST.MF file. Given that you don't have a MANIFEST.MF file, an additional command-line argument can be passed to specify the class containing the main method.

A command like java -cp target/gs-serving-web-content-0.1.0.jar com.pkg.subpkg.MainClass would serve the purpose.

Solution 8 - Java

I had the spring-boot-maven-plugin but still I was getting the error regarding main class.

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

Finally I had to use the maven-jar-plugin and add the mainClass

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-jar-plugin</artifactId>
		<version>2.4</version>
		<configuration>
		  <archive>
		    <manifest>
			<mainClass>com.org.proj.App</mainClass>
		    </manifest>
		  </archive>
		</configuration>
    </plugin>

And it was good to go!

Solution 9 - Java

Use spring-boot-maven-plugin

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

and be sure you set property start-class in the pom.xml

<properties>
      <!-- The main class to start by executing "java -jar" -->
      <start-class>org.example.DemoApplication</start-class>
</properties>

Alternatively, the main class can be defined as the mainClass element of the spring-boot-maven-plugin in the plugin section of your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>             
            <executions>
                 <execution>
                     <goals>
                         <goal>repackage</goal>
                     </goals>
                     <configuration>
                         <classifier>spring-boot</classifier>
                         <mainClass>${start-class}</mainClass>
                     </configuration>
                 </execution>
             </executions>
        </plugin>
    </plugins>
</build> 

Solution 10 - Java

If your plugin configuration is like mine (below), then, use mvn package spring-boot:repackage.

<build>
    <pluginManagement>
        <plugins>
		    <plugin>
		        <groupId>org.springframework.boot</groupId>
		        <artifactId>spring-boot-maven-plugin</artifactId>
		        <executions>
		            <execution>
		                <goals>
		                    <goal>repackage</goal>
		                </goals>
		            </execution>
		        </executions>
		    </plugin>
		</plugins>
	</pluginManagement>
</build>

Happy learning.

Solution 11 - Java

You need to set your main class on MANIFEST.MF file:

Main-Class: com.example.MainClassApplication

Solution 12 - Java

You can use

>mvn package spring-boot:repackage > > hit this command before target dir then use jar file

.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimoneView Question on Stackoverflow
Solution 1 - JavaMradul PandeyView Answer on Stackoverflow
Solution 2 - JavaAtish NarlawarView Answer on Stackoverflow
Solution 3 - JavaMartin SeelerView Answer on Stackoverflow
Solution 4 - JavaMusab QamriView Answer on Stackoverflow
Solution 5 - Javauser2173738View Answer on Stackoverflow
Solution 6 - JavaDrorView Answer on Stackoverflow
Solution 7 - JavaGauravView Answer on Stackoverflow
Solution 8 - Javaveer7View Answer on Stackoverflow
Solution 9 - JavatycoonView Answer on Stackoverflow
Solution 10 - JavaBandham ManikantaView Answer on Stackoverflow
Solution 11 - JavabryanView Answer on Stackoverflow
Solution 12 - JavaRajat KumarView Answer on Stackoverflow