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

12

60

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?

Subhead answered 27/10, 2013 at 16:20 Comment(1)
see #575094Antiquary
S
157

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

Serpigo answered 2/9, 2015 at 4:50 Comment(6)
Simply adding the above code and using mvn package did not work for me. I had to use mvn package spring-boot:repackage ,which created the standalone application.Vilma
When i did this my integrationtests failed due to NoClassDeff... Adding <phase>post-integration-test</phase> to the execution solved it for meAlumna
mvn package spring-boot:repackage resolved my issue. Thanks for the doc attached above.Mobley
mvn package worked for me. I didn't have to do mvn spring-boot:repackage. I am using spring-boot 1.5.9.RELEASE + java 8 on mac os. If you have do mvn spring-boot:repackage, then there is no need to add the repackage goal in your pom.xml. The whole point of adding the repackage goal in pom.xml is that maven will automatically call spring-boot:package when you call mvn package.Drawl
If you properly inherit from spring-boot-starter-parent, then you won't have to do the repackage step.Soninlaw
It's not an obligation to inherit from spring-boot-starter-parent. Especially in big project developers come with their own parent pom for managing dependencies and controlling jar hell.Serpigo
A
21

You might be missing Spring Boot Maven Plugin.

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Autobiographical answered 14/9, 2015 at 19:33 Comment(0)
B
3

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>
Bureaucracy answered 27/10, 2013 at 16:46 Comment(0)
C
3

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.

Chenab answered 14/9, 2018 at 13:18 Comment(0)
A
1

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

Antiquary answered 27/10, 2013 at 16:37 Comment(0)
M
1

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

Multilateral answered 16/10, 2017 at 17:37 Comment(2)
You should not do this manually. When configured properly, spring-boot plugin creates this for you. It is puzzling though that not the same solution works for everyone. I had to add the executions part to the plugin e.g. else it failed.Leolaleoline
i didn't create it manually, (see mentioned in step 3). but yes, not sure what caused this. it's been a while ...Multilateral
B
1

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.

Bellybutton answered 14/3, 2020 at 12:42 Comment(0)
T
0

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!

Teratology answered 12/6, 2019 at 10:23 Comment(0)
K
0

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> 
Kasandrakasevich answered 9/8, 2019 at 11:32 Comment(0)
T
0

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.

Thirion answered 3/7, 2020 at 14:19 Comment(0)
D
0

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

Main-Class: com.example.MainClassApplication
Denumerable answered 10/2, 2021 at 13:32 Comment(0)
A
0

You can use

mvn package spring-boot:repackage

hit this command before target dir then use jar file

.

Augite answered 29/12, 2021 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.