java.lang.LinkageError或java.lang.NoClassDefFoundError

时间:2016-07-04 09:49:26

标签: java maven plugins bundle protege

我在为ProtégéDesktop本体编辑器(v5)打包插件时遇到了一些问题,其中包括Reasoner以及一些Views和Tabs。在推理器的构造函数中,我需要检索活动本体的IRI:

MastroReasoner(@Nonnull OWLOntology ontology, @Nonnull MastroConfiguration configuration) throws ReasonerInitializationException {
    super(ontology, configuration, BufferingMode.BUFFERING);
    this.configuration = configuration;
    this.ontology = ontology;
    this.ontologyIRI = ontology.getOntologyID().getOntologyIRI().orNull();
    initReasoner();
}

其中getOntologyIRI()类型为Optional<IRI>。每当我启动推理器时,我都会出现以下错误:

11:26:16.547 [Classification Thread] ERROR o.p.e.o.m.i.OWLReasonerManager - An error occurred during reasoning: com/google/common/base/Optional.
java.lang.NoClassDefFoundError: com/google/common/base/Optional

我修改了我的pom.xml以将依赖项嵌入我的包中,但现在我遇到了另一个错误:

11:41:48.221 [Classification Thread] ERROR o.p.e.o.m.i.OWLReasonerManager - An error occurred during reasoning: loader constraint violation: loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) previously initiated loading for a different type with name "com/google/common/base/Optional".
java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) previously initiated loading for a different type with name "com/google/common/base/Optional"

我一直在搜索上面的错误,一般都在Google上搜索,我发现的所有答案都说问题来自于可能有2个JAR提供相同的类(在同一个包中) 。那么为什么如果我不添加JAR我有java.lang.NoClassDefFoundError

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>it.uniroma1.dis.mastro.protege</groupId>
    <artifactId>mastro-protege-plugin</artifactId>
    <packaging>bundle</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Mastro Protégé Plugin</name>
    <description>Mastro DL-Lite Reasoner for the Protégé Desktop ontology editor</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <symbolic.name>it.uniroma1.dis.mastro.protege</symbolic.name>
        <protege.version>5.0.0</protege.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>edu.stanford.protege</groupId>
            <artifactId>protege-editor-owl</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>edu.stanford.protege</groupId>
            <artifactId>protege-editor-core</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>edu.stanford.protege</groupId>
            <artifactId>protege-common</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.owlapi</groupId>
            <artifactId>owlapi-osgidistribution</artifactId>
            <version>4.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
        <dependency>
            <groupId>it.uniroma1.dis</groupId>
            <artifactId>mastro-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.fifesoft</groupId>
            <artifactId>rsyntaxtextarea</artifactId>
            <version>2.5.8</version>
        </dependency>
        <dependency>
            <groupId>com.bulenkov</groupId>
            <artifactId>iconloader</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.0</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.0.1</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-Activator>org.protege.editor.owl.ProtegeOWL</Bundle-Activator>
                        <Bundle-Category>protege</Bundle-Category>
                        <Bundle-ClassPath>.</Bundle-ClassPath>
                        <Bundle-ContactAddress>**********</Bundle-ContactAddress>
                        <Bundle-Description>${project.description}</Bundle-Description>
                        <Bundle-DocURL>${project.organization.url}</Bundle-DocURL>
                        <Bundle-Name>${project.name}</Bundle-Name>
                        <Bundle-SymbolicName>${symbolic.name};singleton:=true</Bundle-SymbolicName>
                        <Bundle-Vendor>${project.organization.name}</Bundle-Vendor>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Import-Package>
                            javax.swing,
                            javax.swing.*,
                            org.osgi.framework,
                            org.protege.editor.owl,
                            org.w3c.dom,
                            org.w3c.dom.bootstrap,
                            org.w3c.dom.events,
                            org.w3c.dom.ls,
                            org.xml.sax,
                            org.xml.sax.ext,
                            org.xml.sax.helpers,
                            javax.xml.parsers
                        </Import-Package>
                        <Require-Bundle>
                            org.eclipse.equinox.registry,
                            org.eclipse.equinox.common,
                            org.protege.editor.core.application,
                            org.protege.editor.owl,
                            org.semanticweb.owl.owlapi
                        </Require-Bundle>
                        <Embed-Dependency>
                            commons-collections4,
                            guava,
                            iconloader,
                            mastro-api,
                            rsyntaxtextarea,
                        </Embed-Dependency>
                    </instructions>
                </configuration>
                <executions>
                    <execution>
                        <id>bundle-manifest</id>
                        <phase>install</phase>
                        <goals>
                            <goal>manifest</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>it.uniroma1.dis.mastro.protege</groupId>
                                    <artifactId>mastro-protege-plugin</artifactId>
                                    <version>1.0-SNAPSHOT</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${env.PROTEGE_HOME}/plugins/</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

编辑(2016年7月11日)

为了检查问题是否是由我正在使用的库之一引起的,我创建了一个新的Example插件,只包含推理器配置而没有包含库(因此没有额外的JAR可能包含另一个副本com.google.common.base.Optional包。

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>example-protege-plugin</artifactId>
    <packaging>bundle</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Example Protégé Plugin</name>
    <description>Example for the Protégé Desktop ontology editor</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <symbolic.name>it.uniroma1.dis.example.protege</symbolic.name>
        <protege.version>5.0.0</protege.version>
    </properties>

    <developers>
        <developer>
            <name>*********</name>
            <email>*********</email>
            <organization>${project.organization.name}</organization>
            <organizationUrl>${project.organization.url}</organizationUrl>
        </developer>
    </developers>

    <dependencies>
        <dependency>
            <groupId>edu.stanford.protege</groupId>
            <artifactId>protege-editor-owl</artifactId>
            <version>5.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.0.1</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-Activator>org.protege.editor.owl.ProtegeOWL</Bundle-Activator>
                        <Bundle-Category>protege</Bundle-Category>
                        <Bundle-ClassPath>.</Bundle-ClassPath>
                        <Bundle-ContactAddress>pantaleone@dis.uniroma1.it</Bundle-ContactAddress>
                        <Bundle-Name>Example Protege Plugin</Bundle-Name>
                        <Bundle-SymbolicName>${symbolic.name};singleton:=true</Bundle-SymbolicName>
                        <Bundle-Vendor>${project.organization.name}</Bundle-Vendor>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Import-Package>
                            org.osgi.framework,
                            org.protege.editor.owl.*;version="5.0.0",
                        </Import-Package>
                        <Require-Bundle>
                            org.eclipse.equinox.registry,
                            org.eclipse.equinox.common,
                            org.protege.editor.owl,
                            org.semanticweb.owl.owlapi
                        </Require-Bundle>
                    </instructions>
                </configuration>
                <executions>
                    <execution>
                        <id>bundle-manifest</id>
                        <phase>install</phase>
                        <goals>
                            <goal>manifest</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>${project.artifactId}</artifactId>
                                    <version>${project.version}</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${env.PROTEGE_HOME}/plugins/</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

的plugin.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse version="3.0"?>

<plugin>
    <extension id="Example.reasoner.factory" name="Example" point="org.protege.editor.owl.inference_reasonerfactory">
        <name value="Example Reasoner"/>
        <label value="Example Reasoner"/>
        <class value="it.uniroma1.dis.example.protege.reasoner.ExampleReasonerInfo"/>
    </extension>
</plugin>

这是我的JAR的结构:

enter image description here

这是生成的错误:

13:13:39.811 [Classification Thread] ERROR o.p.e.o.m.i.OWLReasonerManager - An error occurred during reasoning: com/google/common/base/Optional.
java.lang.NoClassDefFoundError: com/google/common/base/Optional
    at it.uniroma1.dis.example.protege.reasoner.ExampleReasoner.<init>(ExampleReasoner.java:17) ~[na:na]
    at it.uniroma1.dis.example.protege.reasoner.ExampleReasonerFactory.createReasoner(ExampleReasonerFactory.java:47) ~[na:na]
    at it.uniroma1.dis.example.protege.reasoner.ExampleReasonerFactory.createReasoner(ExampleReasonerFactory.java:9) ~[na:na]
    at org.protege.editor.owl.model.inference.ReasonerUtilities.createReasoner(ReasonerUtilities.java:20) ~[na:na]
    at org.protege.editor.owl.model.inference.OWLReasonerManagerImpl$ClassificationRunner.ensureRunningReasonerInitialized(OWLReasonerManagerImpl.java:427) ~[na:na]
    at org.protege.editor.owl.model.inference.OWLReasonerManagerImpl$ClassificationRunner.run(OWLReasonerManagerImpl.java:382) ~[na:na]
    at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_91]
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Optional not found by it.uniroma1.dis.example.protege [24]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1556) ~[org.apache.felix.main.jar:na]
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:77) ~[org.apache.felix.main.jar:na]
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1993) ~[org.apache.felix.main.jar:na]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_91]
    ... 7 common frames omitted

每当我从我的推理器构造函数中执行以下行时(构造函数方法中存在的唯一行,除了super):

System.out.println("IRI: " + ontology.getOntologyID().getOntologyIRI().orNull());

有什么想法吗?

0 个答案:

没有答案