编辑:GATE 8.4 中不再存在该问题,GATE 中的类加载确实允许每个插件分开,因此一个插件加载的库不会干扰另一个插件加载的库。
问题
我想,我可以重现这个问题。
GATE Developer 当两个插件被加载并且每个插件都使用不同版本的slf4j-api 时,它就会在 GATE Developer 中体现出来。例如 Ontology 插件使用 slf4j 1.5.6 和 Stanford_CoreNLP slf4j 1.7.12。
当尝试创建 Stanford POS Tagger 的新实例时,会出现以下错误消息(请参阅下面的完整日志):
java.lang.LinkageError:加载程序约束违规:解析方法时“org.slf4j.impl ...
GATE 8.2 build 5482 started at Mon Jul 04 21:54:09 CEST 2016
and using Java 1.8.0_91 Oracle Corporation on Windows 8.1 amd64 6.3.
CREOLE plugin loaded: file:/C:/Program%20Files/gate-8.2-build5482-BIN/plugins/Stanford_CoreNLP/
CREOLE plugin loaded: file:/C:/Program%20Files/gate-8.2-build5482-BIN/plugins/Ontology/
org.xml.sax.helpers.DefaultHandler is available via both the system classpath and a plugin; the plugin classes will be ignored
SLF4J: The requested version 1.5.6 by your slf4j binding is not compatible with [1.6, 1.7]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of gate/util/GateClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of gate/util/GateClassLoader) for the method's defining class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type org/slf4j/ILoggerFactory used in the signature
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:335)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:283)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:304)
at edu.stanford.nlp.io.IOUtils.<clinit>(IOUtils.java:42)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:765)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:298)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:263)
at gate.stanford.Tagger.init(Tagger.java:129)
at gate.Factory.createResource(Factory.java:432)
at gate.gui.NewResourceDialog$4.run(NewResourceDialog.java:270)
at java.lang.Thread.run(Unknown Source)
解决方案
我能想到三种可能的解决方案:
1) 消除不必要的插件
你真的需要两个不兼容版本的 slf4j 插件吗?如果没有,只需卸载您不需要的插件(一定要重新启动 GATE),问题就会消失。
2) 防止加载 ONE 的插件的 slf4j jars
这个解决方案比下一个更脏(因为修改后的插件不能单独工作),但作为一个快速修复应该足够了。选择其中一个插件并从插件的creole.xml 文件中删除 slf4j 条目。同样,GATE 重启后问题应该消失了。
这对于 Ontology 插件来说非常简单:(注意注释掉的行)
<?xml version="1.0"?>
<CREOLE-DIRECTORY>
<JAR>lib/commons-httpclient-3.1.jar</JAR>
<JAR>lib/owlim-lite-5.4.jar</JAR>
<!-- <JAR>lib/slf4j-api-1.5.6.jar</JAR>
<JAR>lib/slf4j-jdk14-1.5.6.jar</JAR> -->
<JAR>lib/openrdf-sesame-2.7.9-onejar.jar</JAR>
<JAR SCAN="true">Ontology.jar</JAR>
</CREOLE-DIRECTORY>
对于 Stanford_CoreNLP 插件,它更复杂,因为它使用 Apache Ivy 加载 slf4j jar,并且必须在 ivy.xml 文件中排除它们 (@987654328 @,注意文件底部添加的行<exclude org="org.slf4j"/>)
<ivy-module version="2.0">
<info
organisation="uk.ac.gate.plugins"
module="stanford_corenlp"
revision="8.2-SNAPSHOT">
<description homepage="https://github.com/GateNLP/gateplugin-Stanford_CoreNLP/" />
</info>
<configurations>
<conf name="default" />
</configurations>
<dependencies defaultconf="*->master(default),runtime(default)" >
<dependency org="edu.stanford.nlp" name="stanford-corenlp" rev="3.6.0" />
<exclude org="org.slf4j"/>
</dependencies>
</ivy-module>
3) 统一冲突的 slf4j 版本
这似乎是一个干净的解决方案,但它比我预期的要复杂得多,因为即使两个插件都使用相同版本的 slf4j,问题仍然存在。问题可能存在于 GATE 类加载机制的更深处,唯一“为我工作”的方式是如何统一 slf4j 版本,将它们排除在所有冲突的插件中并添加 slf4j jar(例如来自 Ontology 插件的那些) ) 到 GATE 的 lib 文件夹。