How do I resolve this error: java.lang.NoSuchFieldError: UTF_32BE?

How do I resolve this error: java.lang.NoSuchFieldError: UTF_32BE?

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

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)

出现此问题是因为您的 class 路径中的 Apache Commons IO 版本与 Owl API.

我的计算如下...

我读到了异常。它提到 UTF_32BE 不是一个字段,并说问题发生在 class 和方法中,所以我为 Owl API 的 [= 挖出了 the source code 14=]方法:

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

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

import org.apache.commons.io.ByteOrderMark;

如果您查看 the sourceByteOrderMark,它的字段定义如下:

/**
 * UTF-32BE BOM (Big-Endian)
 * @since 2.2
 */
public static final ByteOrderMark UTF_32BE = new ByteOrderMark("UTF-32BE", 0x00, 0x00, 0xFE, 0xFF);

这表明您的 class 路径上的 Apache Commons IO 早于 2.2 版本。