2017-07-03 326 views
1

春天引導應用程序版本1.5.3.RELEASESpringBoot:運行的System.loadLibrary兩次

在主類我稱之爲的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) 
... 

爲什麼它被調用兩次?

+1

可能有另一個類在運行時加載庫。如果你刪除這個加載,庫是否加載? – davidxxx

+0

但問題是,這個特定的靜態塊運行兩次 - 如果我把記錄器上面System.loadLibrary(...)我有2個日誌在控制檯 –

+0

如果我刪除System.loadLibrary(...),lib不加載應用程序和OpenCV上的任何調用失敗 –

回答

1

解決的辦法是把System.loadLibrary(Core.NATIVE_LIBRARY_NAME);在它自己的配置類中:

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

謝謝你 – mesutpiskin