springboot maven项目打包SAPJCO3.JAR

下载SAPJCO3

SAPJCO3 3.1.2

springboot项目加入本地JAR包依赖

pom.xml

<!-- 引入sapjco3.jar -->
<dependency>
    <groupId>com.sap.conn.jco</groupId>
    <artifactId>sapjco3</artifactId>
    <version>3.1.2</version>
    <scope>system</scope>
    <systemPath>C:/SAPJCO/sapjco3.jar</systemPath>
</dependency>
...
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 加入以下代码,否则不会将dependency.scope=system的依赖项打包 -->
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>
    </plugins>
</build>

启动报错

Caused by: java.lang.ExceptionInInitializerError:
 JCo initialization failed with java.lang.ExceptionInInitializerError:
  Illegal JCo archive "sapjco3-3.1.2.jar".
   It is not allowed to rename or repackage the original archive "sapjco3.jar".

原因分析

  1. 使用maven打包时会将依赖项jar包改名加上版本号
    打包后可以看到jar包中对应文件为BOOT-INFlibsapjco3-3.1.2.jar
  2. sapjco3.0.11之后版本加入对sapjco3.jar包校验,如果文件名不符合启动项目报错
    源码:com.sap.conn.jco.rt.DefaultJCoRuntime
...
private static String loadJCoLibrary() {
...
if (!osArch.equals("sapjco3.jar") 
	&& !osArch.startsWith("com.sap.conn.jco") 
	&& Package.getPackage("org.apache.maven.surefire.booter") == null 
	&& Package.getPackage("org.eclipse.jdt.internal.junit.runner") == null) {
	throw new ExceptionInInitializerError("Illegal JCo archive "" + osArch + "". It is not allowed to rename or repackage the original archive "" + "sapjco3.jar" + "".");
	}
...
}
...

解决

打包完成后将jar包对应文件改名为sapjco.jar即可

优化

下面文章中使用了另一种方法打包避免改名问题
springboot maven项目引入并打包本地JAR

原文地址:https://www.cnblogs.com/luguojun/p/14294732.html