0

I've written some code that manipulates RDF, and so is dependent on the jena framework. I'm trying to compile / package it using maven. The compilation completes, but when I try and run the generated jar file, I get:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at my.package.name.ClassName.<clinit>(SchemaAPI.java:96)
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.

This has been discussed before (e.g. here and here) but I have modified my pom file to include the shade plugin - and this hasn't solved the issue, and I've hit a bit of a brickwall...

Based on the comments in the discussions in the links above I have checked my META-INF/services/org.apache.jena.system.JenaSubsystemLifecycle file in my my-project-0.1-jar-with-dependencies.jar file and it does indeed contain the reference to TDB (org.apache.jena.tdb.sys.InitTDB) - so I know that this is (almost certainly) the cause of the issue but I'm no nearer to finding a solution...

I'm also including the maven-dependency-plugin and the maven-compiler-plugin - I assume these won't be causing any conflict?

Further info

Some extra info in response to Andy S's comment. The relevant part of my pom file is:

<transformers>
  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    <mainClass>uk.co.domain.mypackage.MyClass</mainClass>
  </transformer>
  <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
  <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
  <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer">
    <addHeader>false</addHeader>
  </transformer>
</transformers>

The relevant file in META-INF only contains one class:

$more META-INF/services/org.apache.jena.system.JenaSubsystemLifecycle
org.apache.jena.tdb.sys.InitTDB
$

I've also tried adding

JenaSystem.init();

at the beginning of my main class, but this hasn't helped

Community
  • 1
  • 1
ChrisW
  • 4,641
  • 6
  • 48
  • 82
  • do you have a github repo or something where you have the code you're trying to run? – Ulises Jun 28 '16 at 16:39
  • Why don't you use a given version from maven central instead of building it? – Nicolas Filotto Jun 28 '16 at 16:43
  • @Ulises no, sorry (company policy about not releasing any code... :/ ) - I could try and make a (relatively) minimal example but wanted to check first there wasn't likely to be an obvious mistake I'm making – ChrisW Jun 28 '16 at 16:47
  • @NicolasFilotto sorry, I'm not sure I understand what you mean? – ChrisW Jun 28 '16 at 16:47
  • Why don't you use jena 3.1.0 for example from maven central instead of building from source (or I misunderstood your question) – Nicolas Filotto Jun 28 '16 at 16:54
  • @NicolasFilotto ah, I'm not building it from source :) – ChrisW Jun 28 '16 at 16:57
  • This is symptomatic of not packing the ServicLoader files correctly. There are several, not one. e.g. for shading, use Check the contents of META-INF/services/org.apache.jena.system.JenaSubsystemLifecycle. Does `META-INF/services/org.apache.jena.system.JenaSubsystemLifecycle` only have the TDB initializer? Theer shoudl be at least 4 classes there. – AndyS Jun 30 '16 at 17:40
  • @AndyS I've added some more info – ChrisW Jul 05 '16 at 12:52
  • The META-INF/services/org.apache.jena.system.JenaSubsystemLifecycle is wrong. Are you using the shade plugin? That what – AndyS Jul 06 '16 at 14:44
  • @AndyS yes I'm using the Shade plugin - I *think* I'm using it correctly (I've never used it before...). Is there anything wrong with the part of the pom file that I've put in the question? Where do I find the jena-fuseki-server pom? I've looked through the maven and epimorphics repositories but can't see it in either of them – ChrisW Jul 07 '16 at 00:12
  • @AndyS ok, I've found the jena-fuseki-server pom. The _only_ thing that is different between the shade plugin configuration between that one and mine is the value of `` - should that stay as `org.apache.jena.fuseki.cmd.FusekiCmd` or as the entry point for _my_ application (i.e. the value which I've got shown in my question)? – ChrisW Jul 07 '16 at 00:41
  • @ChrisW - the fragment looks OK - ServicesResourceTransformer is the one that matter here. This is all quiet senstive to the dependencies as well. – AndyS Jul 07 '16 at 21:19
  • @AndyS how do I find out what the dependencies are? – ChrisW Jul 11 '16 at 13:22
  • @AndyS according to the list at https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin/2.4.3 I've got a copy of all of the dependencies in my .m2 folder – ChrisW Jul 11 '16 at 13:33
  • To find dependencies: `mvn dependency:tree` – AndyS Jul 12 '16 at 14:25

1 Answers1

1

It's not a classpath issue, it's either a misuse or a misconfiguration issue. Basically, you get a null pointer exception at line 33 of EnvTDB, specifically TDB.getContext() returns null. Maybe try running this first:

org.apache.jena.query.ARQ.init();
Sergiu Dumitriu
  • 10,652
  • 3
  • 34
  • 61
  • Try debugging it, find out where it fails. – Sergiu Dumitriu Jun 28 '16 at 17:33
  • The error is the same as in my original question - I'm not really sure how to even start debugging it given it's to do with how maven packages external jars – ChrisW Jun 28 '16 at 22:59
  • I had a similar issue when running some code I wrote using Jena and the ARQ module (nothing to do with TDB though), the getContext() was returning a null pointer, but only when build as a `jar`. Running `org.apache.jena.query.ARQ.init();` solve the issue but I don't really understand why it's only happening when using the `jar` file. – mgc Nov 22 '19 at 22:28