OWL API saveOntology() 调用中出现 NoSuchMethodError

OWL API NoSuchMethodError in saveOntology() call

我正在尝试通过终端调用 OWL API java 程序,但它崩溃了,而当我 运行 它在 IntelliJ 中。

我的主要代码中出现的异常是这样的:

Exception in thread "main" java.lang.NoSuchMethodError: 'boolean org.semanticweb.owlapi.io.RDFResource.idRequiredForIndividualOrAxiom()'
        at org.semanticweb.owlapi.rdf.rdfxml.renderer.RDFXMLRenderer.render(RDFXMLRenderer.java:204)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.render(RDFRendererBase.java:448)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.renderOntologyHeader(RDFRendererBase.java:441)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.render(RDFRendererBase.java:247)
        at org.semanticweb.owlapi.rdf.rdfxml.renderer.RDFXMLStorer.storeOntology(RDFXMLStorer.java:51)
        at org.semanticweb.owlapi.util.AbstractOWLStorer.storeOntology(AbstractOWLStorer.java:142)
        at org.semanticweb.owlapi.util.AbstractOWLStorer.storeOntology(AbstractOWLStorer.java:106)
        at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:1347)
        at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:1333)
        at com.stelios.JavaExplanations.main(JavaExplanations.java:112)

似乎在 RDFResource 对象上调用 idRequiredForIndividualOrAxiom() 没有找到 RDFNode class 继承的方法,但我不知道为什么.

为了 post 这里,我在一个最小的例子中只保留了 saveOntology 调用,抛出的异常与额外的步骤相同:

Exception in thread "main" java.lang.NoSuchMethodError: 'boolean org.semanticweb.owlapi.io.RDFResource.idRequiredForIndividualOrAxiom()'
        at org.semanticweb.owlapi.rdf.rdfxml.renderer.RDFXMLRenderer.render(RDFXMLRenderer.java:204)
        at org.semanticweb.owlapi.rdf.rdfxml.renderer.RDFXMLRenderer.render(RDFXMLRenderer.java:249)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.renderEntity(RDFRendererBase.java:298)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.render(RDFRendererBase.java:292)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.lambda$renderEntities(RDFRendererBase.java:285)
        at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
        at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:177)
        at java.base/java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:1033)
        at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
        at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.renderEntities(RDFRendererBase.java:285)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.renderInOntologySignatureEntities(RDFRendererBase.java:269)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.renderOntologyComponents(RDFRendererBase.java:253)
        at org.semanticweb.owlapi.rdf.RDFRendererBase.render(RDFRendererBase.java:248)
        at org.semanticweb.owlapi.rdf.rdfxml.renderer.RDFXMLStorer.storeOntology(RDFXMLStorer.java:51)
        at org.semanticweb.owlapi.util.AbstractOWLStorer.storeOntology(AbstractOWLStorer.java:142)
        at org.semanticweb.owlapi.util.AbstractOWLStorer.storeOntology(AbstractOWLStorer.java:106)
        at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:1347)
        at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:1333)
        at com.stelios.JavaExplanations.main(JavaExplanations.java:47)

在我的原始代码和最小示例中,我调用 java 时使用:java -cp /home/stelios/java_explanations/target/java_explanations-1.0-SNAPSHOT-jar-with-dependencies.jar com.stelios.JavaExplanations

这是为我重复此行为的最小示例。这是 Java 代码:

package com.stelios;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.*;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.*;
import org.semanticweb.owlapi.model.*;

public class JavaExplanations {
    public static void main(String[] args) throws OWLOntologyCreationException, FileNotFoundException, OWLOntologyStorageException {
        String ontology1 = "/home/stelios/Desktop/huiyfgds/ONTO_ASRTD_hz162pai";
        String ontology2 = "/home/stelios/Desktop/huiyfgds/ONTO_INFRD_hz162pai";

        OWLOntologyManager ontology_manager = OWLManager.createOWLOntologyManager();
        OWLOntology asserted_ontology = ontology_manager.loadOntologyFromOntologyDocument(new File(ontology1));
        ontology_manager.saveOntology(asserted_ontology, new StreamDocumentTarget(new FileOutputStream(ontology2)));
    }
}

这是 IntelliJ 中的 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.stelios.expl</groupId>
    <artifactId>java_explanations</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/net.sourceforge.owlapi/owlexplanation -->
        <dependency>
            <groupId>net.sourceforge.owlapi</groupId>
            <artifactId>owlexplanation</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.owlapi</groupId>
            <artifactId>owlapi-distribution</artifactId>
            <version>5.1.9</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.owlapi</groupId>
            <artifactId>org.semanticweb.hermit</artifactId>
            <version>1.4.5.519</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.32</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.32</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.java</include>
                </includes>
            </resource>
        </resources>
    </build>
    
</project>

我认为这很可能是一些 dependency/version 错误,但我不明白这是怎么回事。我将我需要的所有东西打包在我给出的 jar 文件中作为 class 路径,在 pom.xml 中定义了想要的版本,在这个 jar 中我只能找到一个 org/semanticweb/owlapi/io/RDFResource.class文件。

阅读 and 我考虑过 OWL API 的 2 个不同版本,因为我有另一个 .jar OWL API 版本 3.4。 9 在其中,在目录树中。我移动了文件并重建了 maven 包只是为了确定,并且(正如预期的那样)没有改变。

除了 saveOntology() 调用之外,我的原始程序按预期工作。

唯一不寻常的是 IntelliJ 给我一个 Plugin 'maven-assembly-plugin:' not found 问题,我没有设法以任何方式解决它,并且一直忽略它,因为它不是问题在我需要的任何操作中。 (当然,如果你知道如何解决,请给我建议,但我的主要问题是前面提到的异常)。

编辑 这是 mvn dependency:tree 输出。

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< com.stelios.expl:java_explanations >-----------------
[INFO] Building java_explanations 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ java_explanations ---
[INFO] com.stelios.expl:java_explanations:jar:1.0-SNAPSHOT
[INFO] +- net.sourceforge.owlapi:owlexplanation:jar:5.0.0:compile
[INFO] |  +- net.sourceforge.owlapi:owlapi-api:jar:5.1.19:compile (version selected from constraint [5.0.0,5.9.9])
[INFO] |  |  \- javax.inject:javax.inject:jar:1:compile
[INFO] |  +- net.sourceforge.owlapi:owlapi-tools:jar:5.1.19:compile (version selected from constraint [5.0.0,5.9.9])
[INFO] |  \- net.sourceforge.owlapi:telemetry:jar:5.0.0:compile
[INFO] |     \- net.sourceforge.owlapi:owlapi-parsers:jar:5.1.19:compile (version selected from constraint [5.0.0,5.9.9])
[INFO] +- net.sourceforge.owlapi:owlapi-distribution:jar:5.1.9:compile
[INFO] |  +- net.sourceforge.owlapi:owlapi-compatibility:jar:5.1.9:compile
[INFO] |  |  \- net.sourceforge.owlapi:owlapi-apibinding:jar:5.1.9:compile
[INFO] |  |     +- net.sourceforge.owlapi:owlapi-impl:jar:5.1.9:compile
[INFO] |  |     +- net.sourceforge.owlapi:owlapi-oboformat:jar:5.1.9:compile
[INFO] |  |     \- net.sourceforge.owlapi:owlapi-rio:jar:5.1.9:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-core:jar:2.9.7:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.7:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.7:compile
[INFO] |  +- org.apache.commons:commons-rdf-api:jar:0.5.0:compile
[INFO] |  +- org.tukaani:xz:jar:1.6:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.22:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-model:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-api:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-languages:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-datatypes:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-binary:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-n3:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-nquads:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-ntriples:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-rdfjson:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-jsonld:jar:2.3.2:compile
[INFO] |  |  +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile
[INFO] |  |  |  \- org.apache.httpcomponents:httpcore:jar:4.4.4:compile
[INFO] |  |  \- org.apache.httpcomponents:httpclient-cache:jar:4.5.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-rdfxml:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-trix:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-turtle:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-rio-trig:jar:2.3.2:compile
[INFO] |  +- org.eclipse.rdf4j:rdf4j-util:jar:2.3.2:compile
[INFO] |  +- com.github.jsonld-java:jsonld-java:jar:0.12.0:compile
[INFO] |  |  +- org.apache.httpcomponents:httpclient-osgi:jar:4.5.5:compile
[INFO] |  |  |  +- org.apache.httpcomponents:httpmime:jar:4.5.5:compile
[INFO] |  |  |  \- org.apache.httpcomponents:fluent-hc:jar:4.5.5:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore-osgi:jar:4.4.9:compile
[INFO] |  |     \- org.apache.httpcomponents:httpcore-nio:jar:4.4.9:compile
[INFO] |  +- com.github.vsonnier:hppcrt:jar:0.7.5:compile
[INFO] |  +- com.github.ben-manes.caffeine:caffeine:jar:2.6.1:compile
[INFO] |  +- com.google.guava:guava:jar:22.0:compile (version selected from constraint [18.0,22.0])
[INFO] |  |  +- com.google.errorprone:error_prone_annotations:jar:2.0.18:compile
[INFO] |  |  +- com.google.j2objc:j2objc-annotations:jar:1.1:compile
[INFO] |  |  \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile
[INFO] |  +- com.google.code.findbugs:jsr305:jar:3.0.2:compile (version selected from constraint [2.0.0,4))
[INFO] |  \- commons-io:commons-io:jar:2.5:compile
[INFO] +- net.sourceforge.owlapi:org.semanticweb.hermit:jar:1.4.5.519:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] |  +- org.apache.ws.commons.axiom:axiom-api:jar:1.2.14:compile
[INFO] |  |  +- org.apache.geronimo.specs:geronimo-activation_1.1_spec:jar:1.1:compile
[INFO] |  |  +- org.apache.geronimo.specs:geronimo-javamail_1.4_spec:jar:1.7.1:compile
[INFO] |  |  +- jaxen:jaxen:jar:1.1.4:compile
[INFO] |  |  +- org.apache.geronimo.specs:geronimo-stax-api_1.0_spec:jar:1.0.1:compile
[INFO] |  |  \- org.apache.james:apache-mime4j-core:jar:0.7.2:compile
[INFO] |  +- org.apache.ws.commons.axiom:axiom-c14n:jar:1.2.14:compile
[INFO] |  +- org.apache.ws.commons.axiom:axiom-impl:jar:1.2.14:compile
[INFO] |  |  \- org.codehaus.woodstox:woodstox-core-asl:jar:4.1.4:compile
[INFO] |  |     \- org.codehaus.woodstox:stax2-api:jar:3.1.1:compile
[INFO] |  +- org.apache.ws.commons.axiom:axiom-dom:jar:1.2.14:compile
[INFO] |  +- dk.brics.automaton:automaton:jar:1.11-8:compile
[INFO] |  +- gnu.getopt:java-getopt:jar:1.0.13:compile
[INFO] |  \- net.sf.trove4j:trove4j:jar:3.0.3:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.22:compile
[INFO] +- org.slf4j:slf4j-nop:jar:1.7.32:compile
[INFO] \- org.apache.maven.plugins:maven-assembly-plugin:maven-plugin:3.3.0:compile
[INFO]    +- org.apache.maven:maven-plugin-api:jar:3.0:compile
[INFO]    |  \- org.sonatype.sisu:sisu-inject-plexus:jar:1.4.2:compile
[INFO]    |     \- org.sonatype.sisu:sisu-inject-bean:jar:1.4.2:compile
[INFO]    |        \- org.sonatype.sisu:sisu-guice:jar:noaop:2.1.7:compile
[INFO]    +- org.apache.maven:maven-core:jar:3.0:compile
[INFO]    |  +- org.apache.maven:maven-settings:jar:3.0:compile
[INFO]    |  +- org.apache.maven:maven-settings-builder:jar:3.0:compile
[INFO]    |  +- org.apache.maven:maven-repository-metadata:jar:3.0:compile
[INFO]    |  +- org.apache.maven:maven-model-builder:jar:3.0:compile
[INFO]    |  +- org.apache.maven:maven-aether-provider:jar:3.0:runtime
[INFO]    |  +- org.sonatype.aether:aether-impl:jar:1.7:compile
[INFO]    |  |  \- org.sonatype.aether:aether-spi:jar:1.7:compile
[INFO]    |  +- org.sonatype.aether:aether-api:jar:1.7:compile
[INFO]    |  +- org.sonatype.aether:aether-util:jar:1.7:compile
[INFO]    |  +- org.codehaus.plexus:plexus-classworlds:jar:2.2.3:compile
[INFO]    |  +- org.codehaus.plexus:plexus-component-annotations:jar:1.5.5:compile
[INFO]    |  \- org.sonatype.plexus:plexus-sec-dispatcher:jar:1.3:compile
[INFO]    |     \- org.sonatype.plexus:plexus-cipher:jar:1.4:compile
[INFO]    +- org.apache.maven:maven-artifact:jar:3.0:compile
[INFO]    +- org.apache.maven:maven-model:jar:3.0:compile
[INFO]    +- org.apache.maven.shared:maven-common-artifact-filters:jar:3.1.0:compile
[INFO]    |  \- org.apache.maven.shared:maven-shared-utils:jar:3.1.0:compile
[INFO]    +- org.apache.maven.shared:maven-artifact-transfer:jar:0.11.0:compile
[INFO]    +- org.codehaus.plexus:plexus-interpolation:jar:1.25:compile
[INFO]    +- org.codehaus.plexus:plexus-archiver:jar:4.2.1:compile
[INFO]    |  +- org.apache.commons:commons-compress:jar:1.19:compile
[INFO]    |  \- org.iq80.snappy:snappy:jar:0.4:compile
[INFO]    +- org.apache.maven.shared:file-management:jar:3.0.0:compile
[INFO]    +- org.apache.maven.shared:maven-shared-io:jar:3.0.0:compile
[INFO]    |  +- org.apache.maven:maven-compat:jar:3.0:compile
[INFO]    |  \- org.apache.maven.wagon:wagon-provider-api:jar:2.10:compile
[INFO]    +- org.apache.maven.shared:maven-filtering:jar:3.1.1:compile
[INFO]    |  \- org.sonatype.plexus:plexus-build-api:jar:0.0.7:compile
[INFO]    +- org.codehaus.plexus:plexus-io:jar:3.2.0:compile
[INFO]    +- org.apache.maven:maven-archiver:jar:3.5.0:compile
[INFO]    +- org.codehaus.plexus:plexus-utils:jar:3.3.0:compile
[INFO]    \- commons-codec:commons-codec:jar:1.6:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.339 s
[INFO] Finished at: 2022-01-27T13:06:01+02:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

从 post 的评论中可以看出,我的问题已解决,所以我想我会在这里收集一个结束答案,以免 post 悬而未决。

实际解决方案:如 nicely by @UninformedUser所述,问题是我的依赖项中有冲突的 maven 包版本。使所有内容彼此同步解决了这个问题。

偶然的解决方案:正如我在评论中所写, specifically defining <version>3.3.0</version> for the maven-assembly-plugin happened to solve the issue. But this was only chance, as explained by @Ignazio,只是因为“组装”东西的顺序改变了,覆盖了冲突的包。

非常感谢两位的帮助。