OWLAPI:如果从Jar运行,则不会找到解析器 [英] OWLAPI: Parser not found if run from Jar

查看:181
本文介绍了OWLAPI:如果从Jar运行,则不会找到解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中编写了一个本体导入器,将RDF格式的.owl文件解析为JSON格式的字符串。更具体地说,静态方法 parseOntologyObjectHierarchy 将本体中定义的类层次解析为JSON。如果我从JUnit测试或类的 main 方法(JUnit和类main从IntelliJ IDEA Professional 2017调用)中调用方法,那么一切正常。但是,如果我使用gradle(包括所有依赖项)将我的类打包为jar,我会得到一个 org.semanticweb.owlapi.io.UnparsableOntologyException 。该jar实际上包含所需的 RDFXMLParser



以下是IntelliJ IDEA项目的一个简单示例: https://drive.google.com/open?id=0B10MbhsMWfrydVNKZVJ0QVg1NlE



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



以下是代码:

  public static String parseOntologyObjectHierarchy(字符串文件名)抛出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
存储库{
mavenCentral()
}
依赖关系{
编译组:'net.sourceforge.owlapi',名称:'owlapi-osgidistribution',版本:' 5.1.0'
编译组:'net.sourceforge.owlapi',名称:'owlapi-apibinding',版本:'5.1.0'
编译组:'net.sourceforge.owlapi',名称:'owlapi-parsers',版本:'5.1.0'
编译组:'net.sourceforge.owlapi',名称:'owlapi-impl',版本:'5.1.0'

编译'com.google.code.gson:gson:2.8.0'
编译'net.sourceforge.owlapi:org.semanticweb.hermit:1.3.8.510'
编译组:'org。 glassfish',名称:'javax.json',版本:'1.0.4'
testCompile组:'junit',名称:'junit',版本n:'4.12'
}

任务fatJar(类型:Jar){
manifest {
属性'Implementation-Title':'ExampleCom Ontology Importer',
'Implementation-Version':version,
'Main-Class':'com.example.ontology.OntologyImporter'
}
baseName = project.name +'-all'
来自{configurations.compile.collect {it.isDirectory()? it:zipTree(it)}}
with jar
}

例外文本:

  $ java -jar am-ontology_importer-all-0.1.0-SNAPSHOT.jar 
OWL文件:C:/Users/me/Desktop/Projects/example/example-0.1.0.owl
SLF4J:未能加载类org.slf4j.impl.StaticLoggerBinder。
SLF4J:默认为无操作(NOP)记录器实现
SLF4J:有关详细信息
s,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder。
线程main中的异常org.semanticweb.owlapi.io.UnparsableOntologyException
:问题解析文件:/ C:/Users/me/Desktop/Projects/example/example-0.1.0.owl
无法解析本体。无法找到合适的解析器,或者parsi
ng失败。请参阅下面的解析器日志以获取解释
以下解析器已尝试:
1)org.coode.owlapi.obo12.parser.OWLOBO12Parser@1ca3d04


详细日志:
- -------------------------------------------------- ----------------------------

解析器:org.coode.owlapi.obo12.parser.OWLOBO12Parser @ 1ca3d04
堆栈跟踪:
第1行第22列出现词法错误。遇到:\\\
(10),之后: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.OWLOntologyFactory Impl.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 )
在uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.load(OWLOntolog
yManagerImpl.java:1072)
在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)
在com.example.ontology.OntologyI mporter.loadOntology(OntologyImpo
rtrt.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中的唯一文件。



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


I wrote an ontology importer in Java to parse an RDF-formatted .owl file into a JSON-formatted string. More specifically, the static method parseOntologyObjectHierarchy parses the class hierarchy defined in the ontology into JSON. Everything works fine if I call the method from a JUnit test or the main method of a class (JUnit and the class main are invoked from IntelliJ IDEA Professional 2017). However, if I package my classes as a jar using gradle (including all dependencies), I get an org.semanticweb.owlapi.io.UnparsableOntologyException. The jar actually contains the required RDFXMLParser. Is the classpath in the jar not set properly?

Here is a minimal example IntelliJ IDEA project: https://drive.google.com/open?id=0B10MbhsMWfrydVNKZVJ0QVg1NlE

And here is the corresponding minimal jar: https://drive.google.com/open?id=0B10MbhsMWfrybjJIcDNWd0JFMUk

Here is the code:

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);
}

Here is my 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
}

Here is the exception text:

$ 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)

解决方案

in your minimal jar, the META-INF/services folder contains multiple copies of org.semanticweb.owlapi.io.OWLParserFactory - these are likely coming from your merging of OWLAPI dependencies.

Each module declares in this file which parsers can be found in the module (they are interpreted by ServiceLoader to provide instances); owlapi-distribution contains a merged copy of all the files provided by OWLAPI modules. You need to ensure that that's the only file included in your jar.

The same is true for the other files found in this folder.

这篇关于OWLAPI:如果从Jar运行,则不会找到解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆