如何解决此错误:java.lang.NoSuchFieldError:UTF_32BE?

时间:2018-01-17 10:49:37

标签: java owl ontology owl-api

我正在使用OWL API来读取本体文件。我的代码如下:

OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
IRI iri = IRI.create(new File("src\\main\\webapp\\resources\\inputfile\\20171218 ontology test v0.6.owl"));
System.out.println(iri);
//I am getting error in below line
OWLOntology moduleOWL = manager.loadOntologyFromOntologyDocument(iri);

我收到以下异常。我一直试图解决问题,但没有成功。

file:/D:/Company/Workspace/My%20Data/MyDATA/src/main/webapp/resources/inputfile/20171218%20ontology%20test%20v0.6.owl

Exception in thread "main" java.lang.NoSuchFieldError: UTF_32BE
    at org.semanticweb.owlapi.io.DocumentSources.wrap(DocumentSources.java:248)
    at org.semanticweb.owlapi.io.DocumentSources.getInputStreamFromContentEncoding(DocumentSources.java:284)
    at org.semanticweb.owlapi.io.DocumentSources.connectWithFiveRetries(DocumentSources.java:227)
    at org.semanticweb.owlapi.io.DocumentSources.getInputStream(DocumentSources.java:150)
    at org.semanticweb.owlapi.io.DocumentSources.wrapInput(DocumentSources.java:115)
    at org.semanticweb.owlapi.io.DocumentSources.wrapInputAsReader(DocumentSources.java:79)
    at org.semanticweb.owlapi.io.DocumentSources.wrapInputAsReader(DocumentSources.java:96)
    at org.semanticweb.owlapi.io.AbstractOWLParser.getInputSource(AbstractOWLParser.java:38)
    at org.semanticweb.owlapi.rdf.rdfxml.parser.RDFXMLParser.parse(RDFXMLParser.java:59)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyFactoryImpl.loadOWLOntology(OWLOntologyFactoryImpl.java:188)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.load(OWLOntologyManagerImpl.java:1072)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OWLOntologyManagerImpl.java:1033)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntologyFromOntologyDocument(OWLOntologyManagerImpl.java:973)
    at com.ifour.mydata.test.ConvertXMLtoRDF.getOwlProperty(ConvertXMLtoRDF.java:402)
    at com.ifour.mydata.test.ConvertXMLtoRDF.main(ConvertXMLtoRDF.java:114)

1 个答案:

答案 0 :(得分:2)

出现此问题的原因是您的类路径中有一个与Owl API不兼容的Apache Commons IO版本。

我按照以下方式解决了这个问题......

我读了这个例外。它提到UTF_32BE不是一个字段,并说出问题出现在哪个类和方法中,所以我挖出the source code来查找Owl API的DocumentSources.wrap()方法:

public static InputStream wrap(InputStream delegate) {
    checkNotNull(delegate, "delegate cannot be null");
    return new BOMInputStream(delegate, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE,
        ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE);
}

它引用UTF_*上的各种ByteOrderMark字段,并非所有字段都失败。我发现ByteOrderMark是从Apache Commons IO导入的:

import org.apache.commons.io.ByteOrderMark;

如果您ByteOrderMark查看/** * UTF-32BE BOM (Big-Endian) * @since 2.2 */ public static final ByteOrderMark UTF_32BE = new ByteOrderMark("UTF-32BE", 0x00, 0x00, 0xFE, 0xFF); ,则其字段定义如下:

[TestMethod]
public void ClickBarPayment_ViewExchanges()
{
    DelayedStart(5);
    Application app = null;
    try
    {
        app = Application.Launch(SutPath);
        try
        {
            var window = app.GetWindow("CfeMain");
            var button = (Button)window.Get(SearchCriteria.ByAutomationId("CashButton"), TimeSpan.FromSeconds(60));
            button.Click();
            Assert.AreEqual(false, button.Visible);
        }
        catch (Exception e)
        {
            Assert.Fail(e.Message);
        }
    }
    finally
    {
        app?.Close();
    }
}

这表明您的类路径上有一个早于2.2版本的Apache Commons IO。