如何让使用maven产生的jar包直接运行

本文介绍如何使用Maven-shade-plugin插件生成可执行的JAR包,并解决因签名文件导致的安全异常错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文首先借鉴网上的内容,同时补充不足。

借鉴内容:

执行mvn clean package生成的JAR包默认是不可执行的,因为带有main方法的信息不会被添加到manifest中。使用java -jar运行该包时,报错如下:

  no main manifest attribute, in original-helloworld-1.0-SNAPSHOT.jar

为了生成可执行的JAR包,需要借助maven-shade-plugin插件,生成可执行JAR包只是该插件的功能之一,其他功能见官网,配置示例网址为:

http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

关键的一行在于mainClass,它重新设定了程序运行的入口地址,其内容就是我们编写的类名称。

采用maven官网的内容配置之后,执行时发生:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
    at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:330)
    at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:263)
    at java.util.jar.JarVerifier.processEntry(JarVerifier.java:318)
    at java.util.jar.JarVerifier.update(JarVerifier.java:230)
    at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
    at java.util.jar.JarFile.getInputStream(JarFile.java:450)
    at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:977)
    at sun.misc.Resource.cachedInputStream(Resource.java:77)
    at sun.misc.Resource.getByteBuffer(Resource.java:160)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

这个问题的解决需要在pom.xml文件中加入内容:

For those who got this error when trying to create an uber-jar with maven-shade-plugin, the solution is to exclude manifest signature files by adding the following lines to the plugin configuration:

<configuration>
    <filters>
        <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
        </filter>
    </filters>
    <!-- Additional configuration. -->
</configuration>

添加完成后就是这样一个结构:

<build>
	  <plugins>
		  <plugin>
			  <groupId>org.apache.maven.plugins</groupId>
			  <artifactId>maven-shade-plugin</artifactId>
			  <version>3.1.1</version>
			  <executions>
				  <execution>
					  <phase>package</phase>
					  <goals>
						  <goal>shade</goal>
					  </goals>
					  <configuration>
						  <transformers>
							  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
								  <mainClass>com.nusmart.rules.server.RulesEngineServer</mainClass>
							  </transformer>
						  </transformers>
						  <filters>
							  <filter>
								  <artifact>*:*</artifact>
								  <excludes>
									  <exclude>META-INF/*.SF</exclude>
									  <exclude>META-INF/*.DSA</exclude>
									  <exclude>META-INF/*.RSA</exclude>
								  </excludes>
							  </filter>
						  </filters>
					  </configuration>
				  </execution>
			  </executions>
		  </plugin>
	  </plugins>
  </build>

再次编译,执行,一切正常了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值