Orazio Contarino Orazio Contarino - 1 month ago 19
Java Question

Jena Fuseki Client

I'm trying to write a simple Jena Fuseki client in Java to send a

SELECT
SPARQL query and get the result.

I've found the following code written by someone else. It keeps crashing at runtime.

I'm using
jena-arq-3.1.0.jar


javac -cp .;jena-arq-3.1.0.jar Main.java
print no errors

java -cp .;jena-arq-3.1.0.jar Main
crash!

Error log



Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/jena/atlas/io/Printable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:78)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:52)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:40)
at Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.jena.atlas.io.Printable
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 16 more


Main.java



import java.net.URL;
import java.util.List;

import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;

class Main {

public static void main(final String[] args) throws Exception {
String queryString=
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"prefix owl: <http://www.w3.org/2002/07/owl#>"+

"SELECT ?subject ?predicate ?object"+
"WHERE {"+
" ?subject ?predicate ?object"+
" } LIMIT 25";

// now creating query object
Query query = QueryFactory.create(queryString);
// initializing queryExecution factory with remote service.
// **this actually was the main problem I couldn't figure out.**
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:3030/FirstEndpoint/sparql", query);

//after it goes standard query execution and result processing which can
// be found in almost any Jena/SPARQL tutorial.
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {

// Result processing is done here.
}
}
finally {
qexec.close();
}
}

}

Answer

You need all necessary libs in the classpath, not only jena-arq.

javac works because you're using only classes from the jena-arq JAR. java doesn't work because at runtime the classes of ARQ refer to other classes contain e.g. in jena-core, jena-iri, etc.

The easiest way would to add all libs from the Jena distribution resp. the lib folder itself to the classpath.