SpringBoot启动Tomcat报错LibraryNotFoundError,Can't load library:tcnative-1.dll

本文详细解析了SpringBoot项目中使用最新SpringCloud和内嵌Tomcat时,遇到的JNI动态链接库未找到的问题。通过分析异常栈信息,指出问题源于Tomcat的connector组件未能找到必要的.dll库文件,提供了官方下载链接及解决方案,即将所需库文件放置于C:WindowsSystem32目录下。

环境:Windows10-64位,SpringCloud是最新的Greenwich.RC2,SpringBoot是2.1.1,内嵌Tomcat 9.0.13

最近搭建的SpringCloud项目,把日志级别设为Debug后,从控制台看到启动时tomcat报无法找到JNI动态链接库的错误,完整的异常栈信息如下:

org.apache.tomcat.jni.LibraryNotFoundError: Can't load library: D:\xxx\bin\tcnative-1.dll, Can't load library: D:\xxx\bin\libtcnative-1.dll, no tcnative-1 in java.library.path, no libtcnative-1 in java.library.path
	at org.apache.tomcat.jni.Library.<init>(Library.java:102) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
	at org.apache.tomcat.jni.Library.initialize(Library.java:206) ~[tomcat-embed-core-9.0.13.jar:9.0.13]
	at org.apache.catalina.core.AprLifecycleListener.init(AprLifecycleListener.java:198) [tomcat-embed-core-9.0.13.jar:9.0.13]
	at org.apache.catalina.core.AprLifecycleListener.isAprAvailable(AprLifecycleListener.java:107) [tomcat-embed-core-9.0.13.jar:9.0.13]
	at org.apache.catalina.connector.Connector.<init>(Connector.java:80) [tomcat-embed-core-9.0.13.jar:9.0.13]
	at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:164) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:181) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) [spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at cc.liudun.SpringCloudDemoApplication.main(SpringCloudDemoApplication.java:21) [classes/:na]

这是因为Tomcat中的connector为了提高性能,采用了加载与操作系统绑定(非跨平台)的本地库的方式,比如Windows系统中就是.dll动态链接库。上述异常中找不到的两个.dll库文件,默认会去Tomcat的bin目录下去找,但是由于SpringBoot的Tomcat是嵌入式的,没有这两个.dll,所以只好自己去官网下载,到如下官方地址下载,并放到C:\Windows\System32\目录下:

http://archive.apache.org/dist/tomcat/tomcat-connectors/native/1.2.14/binaries/

 

当出现“D:\Java\Tools\apache - tomcat - 9.0.111\bin\tcnative - 1.dll: Can't load AMD 64 - bit .dll on a IA 32 - bit platform”错误时,意味着在32位平台尝试加载64位的`tcnative - 1.dll`文件,可通过以下方法解决: ### 方法一:更换为32位的`tcnative - 1.dll`文件 1. 从官方渠道或可靠的资源网站下载与32位平台兼容的`tcnative - 1.dll`文件。 2. 将下载好的32位`tcnative - 1.dll`文件替换掉`D:\Java\Tools\apache - tomcat - 9.0.111\bin`目录下原有的64位`tcnative - 1.dll`文件。 3. 重新启动Tomcat,查看问题是否解决。 ### 方法二:使用与平台位数匹配的Tomcat版本 1. 卸载当前64位的Tomcat版本。 2. 下载并安装32位的Tomcat版本,确保其与当前系统的32位平台兼容。 3. 重新配置新安装的32位Tomcat,并启动服务,查看问题是否解决。 ### 方法三:将`tcnative - 1.dll`文件拷贝到Java库路径 1. 查看`java.library.path`环境变量的值,该值指定了Java查找本地库的路径。可以通过以下Java代码查看: ```java public class PrintJavaLibraryPath { public static void main(String[] args) { System.out.println(System.getProperty("java.library.path")); } } ``` 2. 将`D:\Java\Tools\apache - tomcat - 9.0.111\bin`目录下的`tcnative - 1.dll`文件拷贝到`java.library.path`中的任意一个路径下。例如,如果`java.library.path`包含`C:\Tools\jdk1.6.0_29\bin`,则将`tcnative - 1.dll`文件拷贝到该目录下。 3. 重新启动Tomcat,查看问题是否解决。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六吨代码

肝到半夜了,赏五毛行不?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值