java - Spring Boot - .dll 已加载到具有两个项目的另一个类加载器中

标签 java spring spring-boot tomcat

我创建了两个 Spring Boot 项目。
一个是 test项目和另一个是我的 working项目

测试项目:

使用 System.load 我将 .dll 加载到目录
.dll 位于文件夹 test 项目中

工作项目:

现在我正在尝试加载相同的 .dll
.dll 放在我的工作文件夹中

所以每个项目都有自己的 .dll

我总是出错:

java.lang.UnsatisfiedLinkError dir already loaded in another classloader



我是否必须从测试项目中“卸载”第一个 .dll?

tomcat 是否有任何位置为我的测试项目 session 加载 .dll 以便我无法为我的工作项目再次加载 .dll?

或者

如何加载“已加载”库?
Exception in thread "restartedMain" java.lang.UnsatisfiedLinkError: Native Library C:\Dev\workspace\lightserver\src\main\libs\huesdk.dll already loaded in another classloader
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1907)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
at java.lang.Runtime.load0(Runtime.java:809)
at java.lang.System.load(System.java:1086)

最佳答案

spring中集成外部dll时出现,抛出java.lang.UnsatisfiedLinkError异常:

Exception in thread "restartedMain" java.lang.UnsatisfiedLinkError: Native Library *\opencv_java400.dll already loaded in another classloader
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1907)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at com.jinbill.ocr.OcrApplication.<clinit>(OcrApplication.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:15)
使用spring-boot-devtools,springboot会在运行过程中重新加载上下文(启动时会重新加载一次),所以会提示加载链接库,直接在加载的动态链接库所在的位置加上try catch就可以了线:
@SpringBootApplication
public class AppApplication {
    static {
        try {
            System.loadLibrary(NATIVE_LIBRARY_NAME);
        } catch (UnsatisfiedLinkError ignore) {
            / / After using spring-dev-tools, the context will be loaded multiple times, so here will throw the exception that the link library has been loaded.
            / / If there is this exception, the link library has been loaded, you can directly swallow the exception.
        }
    }
    
    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class, args);
    }
}

关于java - Spring Boot - .dll 已加载到具有两个项目的另一个类加载器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49629917/

相关文章:

spring - 何时使用 Spring Web Flow?

java - 实体关系未正确填充

java - Spring Boot 和配置文件

java - JPanel、JFrame、BoxLayout 问题

java - 如何在更新数据库之前从字符串中删除 $ 符号

java - 为什么要使用 Mockito?

java - Spring Cache 与 JPA 和规范

java - 发起 RFQ 失败 : noTradersAvailable_tse

java - Swing 定时器的问题

spring - 内容类型 'text/plain;charset=UTF-8' 在 RestController 类中的 Spring Boot 中不支持错误