maven: no main manifest attribute

18,691

Shade plugin is much easier to use :

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</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>main.Main</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
Share:
18,691
Levon Minasian
Author by

Levon Minasian

Hello! I am Levon. Currently studying for Bachelor's degree in Pure Math at Higher School of Economics.

Updated on June 22, 2022

Comments

  • Levon Minasian
    Levon Minasian almost 2 years

    I am working on my web java project. When I try to run java jar file built by maven, I get an error:

    no main manifest attribute, in "project name".
    

    I think the reason is maven can't find my main class. I have created test project with this hierarchy:

    /test
        /src
            /main
                 Main.java
        pom.xml
    

    The pom.xml file contains this:

    <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>FirstProject</groupId>
    <artifactId>FirstProject</artifactId>
    <version>1.0</version>
    
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugin</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
    
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                        <outputDirectory>${basedir}</outputDirectory>
                        <finalName>server</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                     <archive>
                            <manifest>
                                <mainClass>main.Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
        </plugin>
        </plugins>
    </build>
    

    Also get this error in test project. I tried to google it, everybody advise to add in pom.xml, but I already have done this...(it is not a solution, in my case). How can I solve this problem?

  • Levon Minasian
    Levon Minasian about 5 years
    I got 2 jar files: FirstProject-1.0.jar and original-FirstProject-1.0.jar. If I try to run first of them, I get Error: Could not find or load main class main.Main Caused by: java.lang.ClassNotFoundException: main.Main If another: no main manifest attribute, in original-FirstProject-1.0.jar
  • Essex Boy
    Essex Boy over 2 years
    @LevonMinasian you need to replace "main.Main" with your Main class for the correct entry point.