执行OWL API时遇到错误

时间:2015-05-26 13:04:44

标签: java owl

我正在尝试执行以下代码。

import java.io.File;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.AddAxiom;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;

public class Snippet {
public static void main(String[] args) throws OWLOntologyCreationException {
    File file = new File(
            "file:///c/Users/DTN/Desktop/Final SubmissionFilteringMechanism_Ontology.owl");
    OWLOntologyManager m = OWLManager.createOWLOntologyManager();
    OWLDataFactory f = OWLManager.getOWLDataFactory();
    OWLOntology o;
    o = m.loadOntologyFromOntologyDocument(file);
    OWLClass clsA = f.getOWLClass(IRI.create("urn:test#ClassA"));
    OWLClass clsB = f.getOWLClass(IRI.create("urn:test#ClassB"));
    OWLAxiom ax1 = f.getOWLSubClassOfAxiom(clsA, clsB);
    AddAxiom addAxiom1 = new AddAxiom(o, ax1);
    m.applyChange(addAxiom1);
    for (OWLClass cls : o.getClassesInSignature()) {
        System.out.println(cls.getIRI());
    }
    m.removeOntology(o);
}
}

产生以下错误。

  

线程“main”中的异常java.lang.NoClassDefFoundError:   com / google / inject / Provider at   java.lang.ClassLoader.defineClass1(Native Method)at   java.lang.ClassLoader.defineClass(未知来源)at   java.security.SecureClassLoader.defineClass(未知来源)at   java.net.URLClassLoader.defineClass(未知来源)at   java.net.URLClassLoader.access $ 100(未知来源)at   java.net.URLClassLoader $ 1.run(未知来源)at   java.net.URLClassLoader $ 1.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.net.URLClassLoader.findClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   java.lang.ClassLoader.defineClass1(Native Method)at   java.lang.ClassLoader.defineClass(未知来源)at   java.security.SecureClassLoader.defineClass(未知来源)at   java.net.URLClassLoader.defineClass(未知来源)at   java.net.URLClassLoader.access $ 100(未知来源)at   java.net.URLClassLoader $ 1.run(未知来源)at   java.net.URLClassLoader $ 1.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.net.URLClassLoader.findClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   test.main(test.java:18)引起:java.lang.ClassNotFoundException:   com.google.inject.Provider at   java.net.URLClassLoader.findClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)... 25更多

请一些人帮帮我。提前谢谢。

2 个答案:

答案 0 :(得分:4)

你在课程路径中包含哪些罐子?

看起来你错过了一些google guice依赖项。我建议使用一些依赖管理工具,如maven或gradle

例如,我通过使用maven依赖

解决了这个问题
  

net.sourceforge.owlapi:owlapi-osgidistribution:4.0.2

在一个以Snippet类为主类的maven项目中。

<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>fake</groupId>
<artifactId>fake</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
  <dependency>
    <groupId>net.sourceforge.owlapi</groupId>
    <artifactId>owlapi-osgidistribution</artifactId>
    <version>4.0.2</version>
  </dependency>
</dependencies>

如果要在不使用依赖关系管理器的情况下运行它,请确保在类路径中包含以下jar(使用maven依赖关系树命令获取以下列表)

[INFO] \- net.sourceforge.owlapi:owlapi-osgidistribution:jar:4.0.2:compile
[INFO]    +- org.openrdf.sesame:sesame-model:jar:2.7.12:compile
[INFO]    |  \- org.openrdf.sesame:sesame-util:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-api:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-languages:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-datatypes:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-binary:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-n3:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-nquads:jar:2.7.12:compile
[INFO]    |  \- commons-io:commons-io:jar:2.4:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-ntriples:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-rdfjson:jar:2.7.12:compile
[INFO]    |  \- com.fasterxml.jackson.core:jackson-core:jar:2.2.1:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-rdfxml:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-trix:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-turtle:jar:2.7.12:compile
[INFO]    +- org.openrdf.sesame:sesame-rio-trig:jar:2.7.12:compile
[INFO]    +- com.github.jsonld-java:jsonld-java-sesame:jar:0.5.0:compile
[INFO]    |  \- com.github.jsonld-java:jsonld-java:jar:0.5.0:compile
[INFO]    |     +- com.fasterxml.jackson.core:jackson-databind:jar:2.3.3:compile
[INFO]    |     |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.3.0:compile
[INFO]    |     +- org.apache.httpcomponents:httpclient-cache:jar:4.2.5:compile
[INFO]    |     +- org.apache.httpcomponents:httpclient:jar:4.2.5:compile
[INFO]    |     |  +- org.apache.httpcomponents:httpcore:jar:4.2.4:compile
[INFO]    |     |  \- commons-codec:commons-codec:jar:1.6:compile
[INFO]    |     \- org.slf4j:jcl-over-slf4j:jar:1.7.7:runtime
[INFO]    +- org.semarglproject:semargl-sesame:jar:0.6.1:compile
[INFO]    |  +- org.semarglproject:semargl-core:jar:0.6.1:compile
[INFO]    |  \- org.semarglproject:semargl-rdfa:jar:0.6.1:compile
[INFO]    |     \- org.semarglproject:semargl-rdf:jar:0.6.1:compile
[INFO]    +- com.google.guava:guava:jar:18.0:compile
[INFO]    +- com.google.inject:guice:jar:4.0-beta:compile
[INFO]    |  +- javax.inject:javax.inject:jar:1:compile
[INFO]    |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO]    +- com.google.inject.extensions:guice-multibindings:jar:4.0-beta:compile
[INFO]    +- com.google.code.findbugs:jsr305:jar:2.0.1:compile
[INFO]    +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO]    +- org.apache.directory.studio:org.apache.commons.io:jar:2.4:compile
[INFO]    \- net.sf.trove4j:trove4j:jar:3.0.3:compile

答案 1 :(得分:-1)

这是一个.zip文件,其中包含OWLAPI 4.0.2的所有必需依赖项。您只需将其解压缩并在项目中包含所有这些.jar文件。

从以下网址下载.zip: https://www.dropbox.com/s/835qjm1xrhefy6s/Dependencias%20extras.rar?dl=0

最好的问候。