SpringBoot:System.loadLibrary运行两次

时间:2017-07-03 08:09:58

标签: spring-boot

我有 Spring Boot 应用版 1.5.3.RELEASE

在主课程中,我调用 System.loadLibrary

@SpringBootApplication
@EnableScheduling
public class BlaApplication {

  static {
      System.out.println("xxxxxxxxxxxxxxxxxx Loading Lib");
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  }

  public static void main(String[] args) {

    SpringApplication.run(BlaApplication.class, args);
  }
}

当应用开始 System.loadLibrary被调用两次

xxxxxxxxxxxxxxxxxx Loading Lib
11:10:51.766 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
11:10:51.771 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
11:10:51.771 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/Users/tomas/workspace/formater/formater-backend/target/classes/]
xxxxxxxxxxxxxxxxxx Loading Lib
Exception in thread "restartedMain" java.lang.UnsatisfiedLinkError: Native Library /usr/local/Cellar/opencv/2.4.13.2/share/OpenCV/java/libopencv_java2413.dylib 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.martoma.BlaApplication.<clinit>(BlaApplication.java:16)
...

为什么要两次调用?

2 个答案:

答案 0 :(得分:1)

解决方案是将 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 放在自己的配置类中:

@Configuration
public class LibLoading {
    static {
       System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }
}

答案 1 :(得分:0)

Java异常处理 - UnsatisfiedLinkError

static {

     try {
            // Attempt to load library.
            System.loadLibrary("XXXXXX"); 
        } catch (UnsatisfiedLinkError error) {
            // Output expected UnsatisfiedLinkErrors.
        } catch (Error | Exception error) {
            // Output unexpected Errors and Exceptions.
        }
}