我有一个 OSGi 应用程序,它可以从命令行启动并正常运行。然而,在生产环境中,这个应用程序必须从用 C 编写的小程序启动。这个程序检查许可证,做其他事情并使用 JNI 和我的应用程序启动 java 虚拟机。这样,我从 log4j 和休眠中得到了奇怪的错误(我启用了 -verbose:class JVM 标志):
[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 [org.apache.felix.framework.BundleWiringImpl@eafb71] whereas object of type
log4j:ERROR "org.apache.log4j.RollingFileAppender" was loaded by [sun.misc.Launcher$AppClassLoader@1cde100].
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 类加载器加载的?我怀疑一些本机代码在 OSGi 应用程序启动之前调用 java(例如日志记录),并且这些加载的类以某种方式被重用导致 ClassCastExceptions - 我如何证明或反驳这一点?