OWLAPI:如果来自 Jar 的 运行,则找不到解析器

OWLAPI: Parser not found if run from Jar

我在 Java 中编写了一个 ontology 导入程序来将 RDF 格式的 .owl 文件解析为 JSON 格式的字符串。更具体地说,静态方法 parseOntologyObjectHierarchy 将 ontology 中定义的 class 层次结构解析为 JSON。如果我从 JUnit 测试调用方法或 class 的 main 方法(JUnit 和 class main 是从 IntelliJ IDEA Professional 2017 调用的),一切正常。但是,如果我使用 gradle(包括所有依赖项)将 classes 打包为 jar,我会得到一个 org.semanticweb.owlapi.io.UnparsableOntologyException。该 jar 实际上包含所需的 RDFXMLParser。是不是classjar里面的路径没有设置好?

这是一个最小的 IntelliJ IDEA 项目示例:https://drive.google.com/open?id=0B10MbhsMWfrydVNKZVJ0QVg1NlE

这是相应的最小 jar:https://drive.google.com/open?id=0B10MbhsMWfrybjJIcDNWd0JFMUk

代码如下:

public static String parseOntologyObjectHierarchy(String filename) throws OWLException {
    System.out.println("OWL file: " + filename);
    OWLOntology ontology = loadOntology(filename);
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    return json = hierarchyToString(ontology, df.getOWLThing());
}

public static OWLOntology loadOntology(String filename) throws OWLOntologyCreationException {
    File ontologyFile = new File(filename);
    if (!ontologyFile.exists() || !ontologyFile.isFile()) {
        throw new IllegalArgumentException("OWL file is not a file");
    }
    OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    OWLOntologyDocumentSource source = new FileDocumentSource(new File(filename), new RDFXMLDocumentFormat());
    return ontologyManager.loadOntologyFromOntologyDocument(source);
}

这是我的 build.gradle:

group 'com.example'
version '0.1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
    mavenCentral()
}
dependencies {
    compile group: 'net.sourceforge.owlapi', name: 'owlapi-osgidistribution', version: '5.1.0'
    compile group: 'net.sourceforge.owlapi', name: 'owlapi-apibinding', version: '5.1.0'
    compile group: 'net.sourceforge.owlapi', name: 'owlapi-parsers', version: '5.1.0'
    compile group: 'net.sourceforge.owlapi', name: 'owlapi-impl', version: '5.1.0'

    compile 'com.google.code.gson:gson:2.8.0'
    compile 'net.sourceforge.owlapi:org.semanticweb.hermit:1.3.8.510'
    compile group: 'org.glassfish', name: 'javax.json', version: '1.0.4'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

task fatJar(type: Jar) {
    manifest {
    attributes 'Implementation-Title': 'ExampleCom Ontology Importer',
            'Implementation-Version': version,
            'Main-Class': 'com.example.ontology.OntologyImporter'
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

这是异常文本:

$ java -jar am-ontology_importer-all-0.1.0-SNAPSHOT.jar
OWL file: C:/Users/me/Desktop/Projects/example/example-0.1.0.owl
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further detail
s.
Exception in thread "main" org.semanticweb.owlapi.io.UnparsableOntologyException
: Problem parsing file:/C:/Users/me/Desktop/Projects/example/example-0.1.0.owl
Could not parse ontology.  Either a suitable parser could not be found, or parsi
ng failed.  See parser logs below for explanation.
The following parsers were tried:
1) org.coode.owlapi.obo12.parser.OWLOBO12Parser@1ca3d04


Detailed logs:
--------------------------------------------------------------------------------

Parser: org.coode.owlapi.obo12.parser.OWLOBO12Parser@1ca3d04
    Stack trace:
Lexical error at line 1, column 22.  Encountered: "\n" (10), after : ""        o
rg.coode.owlapi.obo12.parser.OBOParserTokenManager.getNextToken(OBOParserTokenMa
nager.java:1059)
        org.coode.owlapi.obo12.parser.OBOParser.jj_ntk_f(OBOParser.java:296)
        org.coode.owlapi.obo12.parser.OBOParser.TagValuePair(OBOParser.java:147)

        org.coode.owlapi.obo12.parser.OBOParser.Header(OBOParser.java:110)
        org.coode.owlapi.obo12.parser.OBOParser.parse(OBOParser.java:80)
        org.coode.owlapi.obo12.parser.OWLOBO12Parser.parse(OWLOBO12Parser.java:1
09)
        uk.ac.manchester.cs.owl.owlapi.OWLOntologyFactoryImpl.loadOWLOntology(OW
LOntologyFactoryImpl.java:188)
        uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.load(OWLOntologyMa
nagerImpl.java:1072)
        uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OWLOn
tologyManagerImpl.java:1033)
        uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntologyFromOn
tologyDocument(OWLOntologyManagerImpl.java:982)



        at uk.ac.manchester.cs.owl.owlapi.OWLOntologyFactoryImpl.loadOWLOntology
(OWLOntologyFactoryImpl.java:229)
        at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.load(OWLOntolog
yManagerImpl.java:1072)
        at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OW
LOntologyManagerImpl.java:1033)
        at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntologyFro
mOntologyDocument(OWLOntologyManagerImpl.java:982)
        at com.example.ontology.OntologyImporter.loadOntology(OntologyImpo
rter.java:52)
        at com.example.ontology.OntologyImporter.parseOntologyObjectHierar
chy(OntologyImporter.java:64)
        at com.example.ontology.OntologyImporter.main(OntologyImporter.jav
a:142)

在您的最小 jar 中,META-INF/services 文件夹包含 org.semanticweb.owlapi.io.OWLParserFactory 的多个副本 - 这些可能来自您对 OWLAPI 依赖项的合并。

每个模块在这个文件中声明模块中可以找到哪些解析器(它们由ServiceLoader解释以提供实例); owlapi-distribution 包含 OWLAPI 模块提供的所有文件的合并副本。您需要确保这是您的 jar 中包含的唯一文件。

在此文件夹中找到的其他文件也是如此。