Solve the problem when IntelliJ IDEA executes maven packaging and executes the java -jar command to prompt that there is no main manifest attribute in the jar.

problem scenario

IDEA executes mvn clean package -DskipTest=truethe command or uses tools to Maven菜单perform packaging operations, and then java -jar app.jarafter executing the command, it prompts that there is no main manifest attribute in the jar.


D:\WorkSpace\demo\target>java -jar demo-SNAPSHOT.jar
demo-SNAPSHOT.jar中没有主清单属性

Cause Analysis

This error is usually caused by the generated JAR file missing a main manifest attribute. When running a JAR file using a command, the JVM needs to look for a manifest file java -jarcontaining properties in the JAR file . Main-ClassIf this property is not found, this error will occur.

Confirmation analysis

Open the Jar package and view the jar directory in which it was built. You can see that there is a MANIFEST.MF file. This file is the manifest directory to be searched for when the jar is running. The main manifest data is the main class to be run, that is, the class where the main function entry is located.

Insert image description here
Normally, there will be one in the manifest file:

Main-Class:cn.ybzy.demo.mainDemo

solution

AddMain-Class

Manually create a file containing Main-Classmanifest properties MANIFEST.MFand add it to the JAR file

Open the Jar package and MANIFEST.MFadd Main-Class to the file.

Main-Class:cn.ybzy.demo.mainDemo

Use plugins

1. Use maven-jar-pluginplugins

The plugin is used in the pom.xml file maven-jar-pluginand <mainClass>the properties are specified in it.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.example.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

2. Use maven-shade-pluginplugins

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.example.MainClass</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

Guess you like

Origin blog.csdn.net/qq_38628046/article/details/131117598