Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven: no main manifest attribute

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?

like image 271
Levon Minasian Avatar asked Feb 25 '19 14:02

Levon Minasian


People also ask

What is 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.

How do you fix no main manifest attribute in App jar?

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 is manifest attribute?

The manifest attribute specifies the location of the document's cache manifest. HTML5 introduces application cache, which means that a web application is cached, and accessible without an internet connection.


1 Answers

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>
like image 177
Essex Boy Avatar answered Sep 21 '22 12:09

Essex Boy