我们正在尝试为我们的产品支持插件,而我正在使用反射从jar中加载一个类。
我创建了一个URL,并将所有依赖ClassLoader添加为URL。
我使用该ClassLaoder加载插件的主类。
我得到了一个类的实例和一个方法引用( run()方法)。
当我调用这个方法时,我得到了这个异常:
java.lang.NoClassDefFoundError:未能初始化类org.apache.logging.log4j.util.PropertiesUtil
这个类在log4j-api-2.10.0.jar中,它包含在ClassLoader的类路径中。
我可以在debug中看到类加载器中的URL,所以我知道它被正确引用了。
有时我会得到一个不同的错误,说它不能初始化我的一个类,但那个类没有什么特别的,没有静态或任何东西。
反射加载的主类正在调用使用Log4j 2的第二个类。
当主类run()方法试图实例化第二个类时,就会出现这个问题。
除了Logger之外,这两个类中都没有什么是静态的。
在调试器中,我可以从方法ref中看到,类确实加载了我们创建的URL。
这段代码已经工作了十年,我们唯一改变的是插件升级到了Log4j 2。执行反射插件加载的类只是应用程序类路径中的一个普通类。
任何帮助都将不胜感激。
使用毛毛雨的建议我得到了:
Exception in thread "ServerStatus:update_timer:" java.lang.ExceptionInInitializerErrorException in thread "ServerStatus:update_timer:" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method) at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source) at java.lang.Class.forName(Unknown Source)
at ...
at java.lang.Thread.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.StringCaused by: java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.String
at org.apache.logging.log4j.util.SystemPropertiesPropertySource.forEach(SystemPropertiesPropertySource.java:39)
at org.apache.logging.log4j.util.SystemPropertiesPropertySource.forEach(SystemPropertiesPropertySource.java:39)
at org.apache.logging.log4j.util.PropertiesUtil$Environment.reload(PropertiesUtil.java:330) at org.apache.logging.log4j.util.PropertiesUtil$Environment.reload(PropertiesUtil.java:330)
at org.apache.logging.log4j.util.PropertiesUtil$Environment.<init>(PropertiesUtil.java:322) at org.apache.logging.log4j.util.PropertiesUtil$Environment.<init>(PropertiesUtil.java:322)
at org.apache.logging.log4j.util.PropertiesUtil$Environment.<init>(PropertiesUtil.java:310) at org.apache.logging.log4j.util.PropertiesUtil$Environment.<init>(PropertiesUtil.java:310)
at org.apache.logging.log4j.util.PropertiesUtil.<init>(PropertiesUtil.java:69) at org.apache.logging.log4j.util.PropertiesUtil.<init>(PropertiesUtil.java:69)
at org.apache.logging.log4j.util.PropertiesUtil.<clinit>(PropertiesUtil.java:49) at org.apache.logging.log4j.util.PropertiesUtil.<clinit>(PropertiesUtil.java:49)更新: Log4j 2版本2.10.x及更高版本有错误。装入类反射地揭示了这些bug。切换回2.8.2,上面的异常就消失了。然而,现在我仍然为我的一个类获得了ClassNotFound。
发布于 2018-06-30 05:17:31
将Log4j回退到2.8.2已解决问题。这是Log4j上的一个bug。谢谢你的帮助。很难找到的人。
发布于 2018-06-30 03:21:26
这可能有助于解析java.lang.NoClassDefFoundError
try {
Class.forName("org.apache.logging.log4j.util.PropertiesUtil");
} catch (Exception e) {
e.printStackTrace();
}https://stackoverflow.com/questions/51108101
复制相似问题