Find below step by step instructions on how to talk to a 4store KB via the SPARQL-Procotol in Java, using Dan Hanley’s 4store client libs.

First of all you need to have the following installed :
A java compiler (i have Sun’s Java SDK 1.6.x installed on my laptop)
Maven (to compile Dan Hanley’s 4store client libs).

I have all of the code needed for this example placed on my site here : feel free to grab and use it as you wish.

First of all, I should say that I have had to patch the 4store-client-library, as it seems to chomp all of the carriage returns at the end of the lines (“\n”). This is fine you are using the SPARQL-XML-RESULT format, by I am a tad lazy and much prefer working with the tsv format: tsv requires line breaks.

The git diff of my patch looks like so (and again yes it is a tad dirty) :

diff --git a/src/main/java/uk/co/magus/fourstore/client/ b/src/main/java/uk/co/magus/fourstore/clie
index 6652874..097be43 100644
--- a/src/main/java/uk/co/magus/fourstore/client/
+++ b/src/main/java/uk/co/magus/fourstore/client/
@@ -349,7 +349,7 @@ public class Store {
String response = "";
String str;
while (null != ((str = in.readLine()))) {
- response += str;
+ response += str+"\n";
return response;

This above patch allows me to make use of the TSV output as needed. The patch can be grabbed from here :

Ok, now that I have built the 4store-client.jar file with my patch using the instructions in the git repo. I create an example RDF file, and some example java code which will query the 4store KB. All the files can be found on my site

These are the steps which I have taken to get this little demo up and running :
1. Create 4store KB called lame

4s-backend-setup --node 0 --cluster 1 --segments 2 lame

2. Start lame‘s backend (i.e. start the KB)

4s-backend lame

3. Import the example.rdf file into lame‘s backend (i.e. start the KB)

4s-import lame example.rdf

4. Start HTTPD for the KB named lame

4s-httpd -s -1 lame

And then I need to compile the exampleQuery code I knocked together

javac -cp 4store-client-1.0.jar

Finally, I run the code like so :

java -cp 4store-client-1.0.jar:. exampleQuery

Resulting in the following output:

Am about to query a sparql endpoint
A user name is:"Bob"
A user name is:"Alice"

I hope this helps!


Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.