在groovy 2.1.6项目中包含slf4j-api-1.7.5和slf4j logger(如simple-1.7.5)时,只要加载第一个实例化Logger的类,我就会得到以下异常:
Caught: java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/codehaus/groovy/tools/RootLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type taticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory; used in the signature
java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/codehaus/groovy/tools/RootLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type taticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory; used in the signature
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:299)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
at Server.<clinit>(Server.java:29)
at Test.run(Test.groovy:15)
在详细的jvm输出中挖掘我在打印异常之前找到以下行:
[Loaded org.slf4j.ILoggerFactory from file:/home/user/.gradle/caches/artifacts-26/filestore/org.slf4j/slf4j-api/1.7.5/jar/6b262da268f8ad9eff941b25503a9198f0a0ac93/slf4j-api-1.7.5.jar]
...
[Loaded org.slf4j.ILoggerFactory from file:/home/user/.gradle/caches/artifacts-26/filestore/org.slf4j/slf4j-api/1.7.5/jar/6b262da268f8ad9eff941b25503a9198f0a0ac93/slf4j-api-1.7.5.jar]
似乎Groovy RootLoader和Java AppClassLoader尝试自己加载类ILoggerFactory并最终得到两个“版本”(两者都可能相等,但jvm似乎没有认识到这一点)。 如何阻止其中一个ClassLoader加载ILoggerFactory并最终使用日志工作?