java打包jar
1 打包介绍
jar分为胖包和廋包,廋包不包含引用信息,胖包含引用的jar。这里主要介绍胖包的两种方法和遇到的问题
2 打包教程
2.1 Artifacts
- ide中选择file

- 选择project struct

- 这里选择jar包的程序入口

- 这里选择要引用的jar包

- 这里选择build

2.2 maven
<?xml version="1.0" encoding="UTF-8"?>
<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>org.xx</groupId>
<artifactId>merchant-access</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.abc</groupId>
<artifactId>cryptokit.jni</artifactId>
<version>1.0</version>
<!-- <scope>system</scope>-->
<!-- <systemPath>${project.basedir}/lib/cryptokit.jni-1.0.jar</systemPath>-->
</dependency>
<dependency>
<groupId>com.abc</groupId>
<artifactId>logback-cfca-jdk1.6</artifactId>
<version>1.0</version>
<!-- <scope>system</scope>-->
<!-- <systemPath>${project.basedir}/lib/logback-cfca-jdk1.6-4.2.1.0.jar</systemPath>-->
</dependency>
<dependency>
<groupId>com.abc</groupId>
<artifactId>SADK</artifactId>
<version>1.0</version>
<!-- <scope>system</scope>-->
<!-- <systemPath>${project.basedir}/lib/SADK-3.7.0.1.jar</systemPath>-->
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.psbc.pfpj.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

2.3 问题
这里主要遇到一个报错:
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:317)
at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:259)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:323)
at java.util.jar.JarVerifier.update(JarVerifier.java:234)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:404)
at java.util.jar.JarFile.ensureInitialization(JarFile.java:641)
at java.util.jar.JavaUtilJarAccessImpl.ensureInitialization(JavaUtilJarAccessImpl.java:69)
at sun.misc.URLClassPath$JarLoader$2.getManifest(URLClassPath.java:995)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:456)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:601)
网上搜
Error: A JNI error has occurred, please check your installation and try again都说是java 和 javac的jdk版本不对,但是更换了jdk和电脑都会报这个错。最后搜Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes网上说是因为jar内部相互调用的时候出错
解决办法:
- 1 直接删除jar内部 META-INF 文件夹下面的 SF DSA RSA 文件
- 2 在pom文件里面排除 SF DSA RSA 文件生成
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
2.3 总结
jar的打包方式有很多种,这里主要介绍了大胖包的两种

浙公网安备 33010602011771号