-->
当前位置:首页 > DayDayUp > 正文内容

java Maven 编译jar运行时报错:"XXX--1.0-SNAPSHOT.jar中没有主清单属性"

Luz5个月前 (07-09)DayDayUp1071


报错处理

Maven项目打包时没有设置主类参数,需要增加plugin配置image.png



修改pom.xml文件,在project中添加

<build>
    <plugins>  
  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-jar-plugin</artifactId>  
            <version>2.6</version>  
            <configuration>  
                <archive>  
                    <manifest>  
                        <addClasspath>true</addClasspath>  
                        <classpathPrefix>lib/</classpathPrefix>  
                        <mainClass>xx.xx.SpringStart</mainClass>  
                    </manifest>  
                </archive>  
            </configuration>  
        </plugin>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-dependency-plugin</artifactId>  
            <version>2.10</version>  
            <executions>  
                <execution>  
                    <id>copy-dependencies</id>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>copy-dependencies</goal>  
                    </goals>  
                    <configuration>  
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>main.java.main</mainClass>  <!--修改成自己项目的入口-->
            </manifest>
        </archive>
    </configuration>
</plugin>

    </plugins>  
</build>


重新编译

image.png



运行jar

image.png


正常打印123



寻找mainClass

压缩软件打开jar文件,找到main函数所在class

image.png

其在jar文件中的相对位置即为mainClass标签所填参数

(这个项目中main入口在main.class中,查看main.class相对路径为/main/java/main.class  ,所以pom.xml中mainClass标签值为main.java.main)

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。