Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no main manifest attribute, in jar

I am trying to run a jar created by the maven shade plugin. I am configuring the main class the following way:

<project>
...
<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <manifestEntries>
                <Main-Class>org.comany.MainClass</Main-Class>
                <Build-Number>123</Build-Number>
              </manifestEntries>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

...

But when I try running the jar using java -jar app.jar it gives the following error

 "no main manifest attribute, in  app.jar"

EDIT: I checked the contents of the jar using jar tf app.jar and I see a MANIFEST.MF file. BUt it does not have the entry for main class. How do I make sure the manifest file in jar has this entry apart for adding it in the shade plugin configuration?

like image 890
user_mda Avatar asked Jun 10 '15 13:06

user_mda


People also ask

How do I fix no main manifest attribute?

You might get this error when the Main-Class entry is missing in MANIFEST. MF file. You can put the maven-jar-plugin plugin in pom. xml to fix it.

What does no main manifest attribute?

In a Java project, every executable jar file contains a main method. Usually, it is placed at starting point of the application. To execute a main method by a self-executing jar file, we must have a proper manifest file and wrap it with our project at the proper location.


1 Answers

Maven's shade plugin uses the JAR generated by the jar plugin and adds dependencies on it. Since it seems like the transforms on the shade plugin do not work properly, you just need to set the configuration for the jar-plugin like so:

<build>
  ...
  <plugins>
    ...     
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>com.mypackage.MyClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
  <plugin>
    [Your shade plugin definition without the transformers here]
  </plugin> 
</build>
like image 121
Konstantin Fedorov Avatar answered Sep 19 '22 15:09

Konstantin Fedorov