1
我有一個OSGi應用程序,它可以從命令行啓動並正常運行。然而,在生產環境中,這個應用程序必須從用C編寫的小程序啓動。這個程序檢查許可證,執行其他的東西,並用我的應用程序使用JNI啓動Java虛擬機。就這樣,我從log4j的有奇怪的錯誤和休眠(我已經啓用了-verbose:類JVM標誌):OSGi和JNI的類加載問題
[Loaded org.apache.log4j.Appender from reference:file:/opt/dc/lib/log4j-1.2.15.jar]
[Loaded org.apache.log4j.Appender from file:/opt/dc/lib/log4j-1.2.15.jar]
...
[Loaded org.apache.log4j.RollingFileAppender from file:/opt/dc/lib/log4j-1.2.15.jar]
log4j:ERROR A "org.apache.log4j.RollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable.
log4j:ERROR The class "org.apache.log4j.Appender" was loaded by
log4j:ERROR [[email protected]] whereas object of type
log4j:ERROR "org.apache.log4j.RollingFileAppender" was loaded by [[email protected]].
log4j:ERROR Could not instantiate appender named "ROLLINGFILE".
...
[Loaded org.dom4j.DocumentFactory from reference:file:/opt/dc/lib/dom4j-1.6.1.jar]
...
[Loaded org.dom4j.DocumentFactory from file:/opt/dc/lib/dom4j-1.6.1.jar]
...
java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory
at org.dom4j.DocumentFactory.getInstance(DocumentFactory.java:97)
at org.dom4j.io.DOMReader.<init>(DOMReader.java:38)
at org.hibernate.util.XMLHelper.createDOMReader(XMLHelper.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1615)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1218)
爲什麼這些類加載兩次?爲什麼他們中的一些從Felix加載,一些是從Java classloader加載的?我懷疑某些本地代碼在OSGi應用程序啓動之前調用了java(例如日誌記錄),並且這些加載的類以某種方式重複使用導致ClassCastExceptions - 我該如何證明或反駁這一點?