Search code examples
javaowlontologyowl-api

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


I am using the OWL API to read an ontology file. My code is as below:

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

I am getting the following exception. I have tried all the way to solve the problem, but did not succeed.

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)

Solution

  • The problem occurs because you have a version of Apache Commons IO in your classpath that is incompatible with the Owl API.

    I worked this out as follows...

    I read the exception. It mentioned UTF_32BE not being a field and said in which class and method the problem occurred, so I dug out the source code for Owl API's DocumentSources.wrap() method:

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

    It references various UTF_* fields on ByteOrderMark, not all of which are failing. I found that ByteOrderMark is imported from Apache Commons IO:

    import org.apache.commons.io.ByteOrderMark;
    

    If you look at the source for ByteOrderMark, it has the field defined as follows:

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

    Which suggests that you have an earlier than 2.2 version of Apache Commons IO on your classpath.