1

I am writing a Java app using Apache Jena as a framework to handle RDF. The goal is a consistency check after doing OWL reasoning. The app is already working, but lacks support for TriG files. In the Jena doc it says TriG is supported (https://jena.apache.org/documentation/io/index.html).

This is my code to parse the RDF file:

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.RDFDataMgr;
import java.io.InputStream;

String rdfFilePath = "sample.ttl";
String rdfReaderType = "TURTLE";
Model model = ModelFactory.createDefaultModel();
InputStream inStream = RDFDataMgr.open(rdfFilePath);
model.read(inStream, null, rdfReaderType);

It works perfectly fine for the reader types "TURTLE", "RDF/XML", "N-TRIPLE" and "JSONLD". It does not work for rdfReaderType = "TRIG". I get the following exception:

Exception in thread "main" org.apache.jena.shared.NoReaderForLangException: Reader not found: TRIG
        at org.apache.jena.rdf.model.impl.RDFReaderFImpl.getReader(RDFReaderFImpl.java:61)
        at org.apache.jena.rdf.model.impl.ModelCom.read(ModelCom.java:305)

Then I had the theory that a TriG file maybe cannot be parsed to a model, only a dataset. So I tried the following code:

import org.apache.jena.query.Dataset;
import org.apache.jena.riot.RDFLanguages;

String rdfFilePath = "sample.trig";
Dataset dataset = RDFDataMgr.loadDataset(rdfFilePath, RDFLanguages.TRIG);

But I get the following exception:

Exception in thread "main" java.lang.NullPointerException
        at org.apache.jena.riot.RDFParserBuilder.build(RDFParserBuilder.java:607)
        at org.apache.jena.riot.RDFParserBuilder.parse(RDFParserBuilder.java:540)
        at org.apache.jena.riot.RDFDataMgr.parseFromURI(RDFDataMgr.java:921)
        at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:550)
        at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:517)
        at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:470)
        at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:460)
        at org.apache.jena.riot.RDFDataMgr.loadDataset(RDFDataMgr.java:414)

Thank you for any help!

DHen
  • 38
  • 4
  • Which version of jena? Your dataset example is correct and should work. (Lang is better than RDFLanguages but that is a small point). The error is characteristic of repacking the jars but not correcting handling the service loader files. https://jena.apache.org/documentation/notes/jena-repack.html . System initialization has not happened properly. – AndyS Dec 03 '20 at 21:53
  • Thank you, that was exactly the problem! I used Maven with the Assembly plugin to create one jar file. After exchanging that for the Shade plugin like you hinted both code variants work fine now. – DHen Dec 04 '20 at 11:01
  • Great! I've put my comment in as an answer - could you accept it please? - it helps other who search because of similar problems. – AndyS Dec 04 '20 at 15:36

1 Answers1

1

Your dataset example is correct and should work.

The error is characteristic of repacking the jars but not correcting handling the service loader files.

https://jena.apache.org/documentation/notes/jena-repack.html

If the service loader files are not combined, system initialization does not happen properly.

The effect here is that there is not a setting for the "context" object which then causes a NullPointerException.

AndyS
  • 16,345
  • 17
  • 21