5

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)
soufrk
  • 785
  • 8
  • 21
gonephishing
  • 1,276
  • 1
  • 18
  • 40
  • 1
    The code works fine if just that codeis run. There is probably more in the environment. This error typically occurs when Jena jars are rebundled (e.g. OSGi) or repackaged and the `META-INF/services/org.apache.jena.system.JenaSubsystemLifecycle` files have not been merged properly. – AndyS Apr 21 '16 at 08:08
  • Please provide a complete, minimal example. Something someone else can run unchanged that contains no more than is needed to illustrate the issue. – AndyS Apr 21 '16 at 08:11
  • The code does not load data into memory. – AndyS Apr 21 '16 at 08:12
  • @AndyS, The code runs fine when using eclipse but gives this exception when launching from the terminal. I guess eclipse is bundling/ resolving things on its own which is not happening when using the terminal. I tried launching it from different systems and issue is still there. You can reproduce the issue from here https://github.com/gone-phishing/SDW – gonephishing Apr 21 '16 at 12:08
  • The code mentioned here just loads the rdf dataset as a graph in the specified directory. The remaining part of the code can be found here: https://github.com/gone-phishing/SDW/blob/master/src/main/java/org/sdw/model/JenaModel.java – gonephishing Apr 21 '16 at 12:11
  • 1
    I am getting the same problem and I'm not understanding how to solve this problem, can I get some help ? – Noor Oct 17 '17 at 09:06
  • The actual code to answer the question is here: https://stackoverflow.com/a/53683355/5973334 – Kuzeko Dec 08 '18 at 14:12

3 Answers3

5

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

Nadjib Mami
  • 5,105
  • 9
  • 33
  • 48
AndyS
  • 14,989
  • 15
  • 20
  • I see, I was making a stupid mistake. Thanks for helping me out :) – gonephishing Apr 22 '16 at 15:19
  • 1
    @Andys Hi, So what is the solution for this ? I am bumping into the same problem. How one resolve it. In particular i am working with sbt, and building an assembly jar. – MaatDeamon Sep 13 '17 at 21:44
  • See the https://github.com/apache/jena/blob/master/jena-fuseki2/jena-fuseki-server/pom.xml -- the `ServicesResourceTransformer` which combines ServiceLoader files. if you need to do that manually, because SBT can't, then concatenate the files under META_INF/services/ for all the jars. – AndyS Sep 14 '17 at 22:14
  • 1
    I am getting the same problem and I'm not understanding how to solve this problem, can I get some help ? – Noor Oct 17 '17 at 09:06
  • @Noor open the link mentioned in the answer and add line 78 to your POM file in maven shade plugin section as shown in the above link. Should work fine :) – gonephishing Oct 17 '17 at 12:20
  • @gonephishing, thanks for you reply, I added the line but still getting the same issue, here is a link for my POM file where the shade plugin is defined, https://github.com/noorbakerally/LDPizer/blob/master/pom.xml#L149, can you please have a look at it and tell me the problem, – Noor Oct 17 '17 at 12:49
  • Not very sure though try to have the complete template in place as mentioned in the jena-fuseki link – gonephishing Oct 17 '17 at 19:13
  • @gonephishing, use the entire template with no success, did you changed anything in the template ? – Noor Nov 06 '17 at 22:46
  • Nothing that i remember as of now :( – gonephishing Nov 07 '17 at 14:13
  • If you need the actual code to add to the pom base on the provided link, I've posted it here: https://stackoverflow.com/a/53683355/5973334 (I've tried to suggest an edit to the answer, but got rejected for reasons) – Kuzeko Dec 08 '18 at 14:13
1

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.

1

The accepted answer is actually missing the solution. So here it is:

The linked file is: https://github.com/apache/jena/blob/master/jena-fuseki2/jena-fuseki-server/pom.xml

Of course, what should you get from it?

Here is the complete fragment that you should add to your pom:

<build>     
<plugins>     
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.1</version>

    <executions>    
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>com.example.MainClass</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>

remember to replace the com.example.MainClass with your main class

Kuzeko
  • 1,277
  • 12
  • 34
  • I think that the above code is part of the link mentioned in the accepted answer. But thanks for specifying it here for future references! – gonephishing Dec 10 '18 at 06:58
  • Well, there are few meaningful lines in over 100 lines of code at the link, and you need to figure out what to keep and what not. I found this question because I had this problem, and it took me 30mins, 4 tentatives, and merging comments from other answers and comments here, before coming up with that fragment. So, I think the answer is incomplete without the code. – Kuzeko Dec 11 '18 at 11:58