下載SAPJCO3
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".
原因分析
- 使用maven打包時會將依賴項jar包改名加上版本號
打包後可以看到jar包中對應文件爲BOOT-INF\lib\sapjco3-3.1.2.jar - 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即可