Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jena TDB java.lang.ExceptionInInitializerError

I'm using Jena TDB for loading an RDF dataset and making SPARQL queries against it. I'm using the following maven dependency:

<dependency>
    <groupId>org.apache.jena</groupId>
    <artifactId>apache-jena-libs</artifactId>
    <type>pom</type>
    <version>3.0.1</version>
</dependency>

And here's the java code where I'm trying to create a TDB dataset:

public void loadDirectory(String outputFile){      
    Dataset dataset = TDBFactory.createDataset(directoryPath);      
    Model tdb = dataset.getDefaultModel();      
    FileManager.get().readModel(tdb, outputFile);      
    tdb.close();      
    dataset.close();      
    LOG.info("RDF dataset loaded to memory");      
}      

It's failing on the first line of the function: TDBFactory.createDataset( directoryPath ) with the following error message:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.sdw.model.JenaModel.loadDirectory(JenaModel.java:69)
    at org.sdw.Main.main(Main.java:75)
Caused by: java.lang.NullPointerException
    at org.apache.jena.tdb.sys.EnvTDB.processGlobalSystemProperties(EnvTDB.java:33)
    at org.apache.jena.tdb.TDB.init(TDB.java:250)
    at org.apache.jena.tdb.sys.InitTDB.start(InitTDB.java:29)
    at org.apache.jena.system.JenaSystem.lambda$init$40(JenaSystem.java:114)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:179)
    at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:156)
    at org.apache.jena.system.JenaSystem.init(JenaSystem.java:111)
    at org.apache.jena.tdb.TDBFactory.<clinit>(TDBFactory.java:40)
like image 873
gonephishing Avatar asked Apr 20 '16 21:04

gonephishing


2 Answers

The POM uses the shade plugin. It needs to manage services files (META_INF/services/) with a ServicesResourceTransformer transformer.

Add the following transformed to your POM file:

<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />

See <transformers> here for example: https://github.com/apache/jena/blob/master/jena-fuseki2/jena-fuseki-server/pom.xml

like image 70
AndyS Avatar answered Oct 01 '22 01:10

AndyS


I had the same problem and found that the accepted answer is in general correct but not complete (at least it took me quite a while before I figured out how to apply the tip of the answer correctly). Here is how it works.

1) You have to add the maven-shade plugin to you pom.xml as demonstrated e.g. in: https://github.com/apache/jena/blob/master/jena-fuseki2/jena-fuseki-server/pom.xml

2) Change the link to the main class in the plugin configuration. The main class is provided via the following lines:

<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
          <mainClass>org.apache.jena.fuseki.cmd.FusekiCmd</mainClass>
</transformer>

You have to add you main class in the mainClass tag. When now building the project using the maven build command, you will get a jar called your-project-name-VERSION.jar which is the runnable jar you want to have. If you previously worked with a "jar with dependencies", then make sure to run the new one (which does not include the "with dependencies" in the name anymore) as otherwise you will run into the same problem.

like image 29
BlackHawkLex Avatar answered Oct 01 '22 03:10

BlackHawkLex