Monthly Archives: April 2015

Generate a FOAF Document with Apache Jena

In our first tutorial, we saw how to get started with Apache Jena using Eclipse IDE and we also became familiar with the interface Model and the class ModelFactory. We learned how to write a simple RDF graph with a Resource and a Property. This post will look into generating a sample FOAF document (Friend of Friend, a machine-readable ontology describing people and their activities and relations) by employing Apache Jena.

First of all, I have created a simple FOAF description  by using the Javascript-based application FOAF-a-matic. It is a very straightforward task as you will only need to include the info you would like to be in your FOAF file and click the button ‘FOAF me’ at the bottom of the page. This is the resulting FOAF with the info provided:

[code language=”xml”]
<foaf:PersonalProfileDocument rdf:about="">
<foaf:maker rdf:resource="#me"/>
<foaf:primaryTopic rdf:resource="#me"/>
<admin:generatorAgent rdf:resource=""/&gt;
<foaf:Person rdf:ID="me">
<foaf:name>Ilaria C</foaf:name>
<foaf:homepage rdf:resource=""/&gt;

Our goal is to generate your own FOAF document with the following attribute: foaf : name; foaf : title; foaf : givenname; foaf : family_name; foaf : nick and foaf : homepage. Here is our Jena implementation:

[code language=”java”]
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.vocabulary.FOAF;

public class FOAFGeneration extends Object {

public static void main (String args[]) {

// some definitions
String personURI = ";;
String firstName = "Ilaria";
String familyName = "C";
String title = "Dr";
String nickName = "ila";
String homePage = ";;
String fullName = firstName + " " + familyName;

// create an empty model
Model model = ModelFactory.createDefaultModel();

// create the resource
// and add the properties cascading styleResource
model.createResource(personURI, FOAF.Person)
.addProperty(, fullName)
.addProperty(FOAF.title, title)
.addProperty(FOAF.givenname, firstName)
.addProperty(FOAF.family_name, familyName)
.addProperty(FOAF.nick, nickName)
.addProperty(FOAF.homepage, homePage);

// write the model in Turtle
model.write(System.out, "Turtle");

Here is the expected result from our Eclipse Console:



Getting Started with Apache Jena and Eclipse

Apache Jena is an Open Source Framework for building Semantic Web and Linked Data applications. Jena provides an API to help write Java code that handles RDF, RDFS, RDFa, OWL and SPARQL. It supports serialisation of RDF graph for:

  • relational databases
  • Turtle
  • Notation 3

A similar framework  RDF4J (formerly known as Sesame) queries, processes and analyses RDF constructs (it supports SPARQL and SeEQL), however it does not currently support OWL. This tutorial will be a step-by-step guide on how to attach Jena to an Eclipse project in order to write and print out a sample RDF statement. Step 1 – Create a new Java Project in Eclipse First of all, you will need to create a new Java project in Eclipse. This can be done by selecting File -> New-> Java Project. You will be prompted with the below window where you will specify the name of your project and the chosen execution environment JRE (in our example, we used 1.7): Step1-JenaTutorial   Step 2 – Create your sample Class  Create a sample Class called MyFirstRDFStatement from Jena-Tutorial-Project -> New -> Class. Initially, this class will only include the Class declaration and main method, but we will later be enriched with the necessary code to create a RDF statement and output it as Turtle format. screenshot2   Step 3 Adding the Jena Libraries to your Eclipse Project In order to start writing and reading RDF statements, we need to import the Jena Libraries into our project. Go to Window -> Preferences -> BuildPath -> UserLibraries:  screenshot3 You would have already downloaded the Jena Libraries from here. Create a New User Libraries by clicking on New and specifying your custom name: screenshot4 From the left click on ADD External JARs and navigate to the location where your libraries are located (in my case, they were placed in my Download folder): screenshot13_001   Step 4: Create an empty Model with ModelFactory The first element you will need to include in your MyFirstRDFStatement class is a Model. In Jena terms, a Model is a set, or better, a container for RDF statements. The Model interface is contained in the package com.hp.hpl.jena.rdf.model and it provides methods for creating resources, properties, literals and statements. In our example, the class ModelFactory provides methods for creating a standard kind of Model such as createDefaultModel, which simply creates a fresh Model with some default specification: screenshot10 It should be noted that our Model is currently ’empty’, therefore we would need to create the resource and add its property. Step 5: Create the Resource and its Property In order to create the Resource and Property, you will need to use the Model methods createResource and createProperty, as follows: screenshot11   Step 6: Write a serialised representation of the Model The last step of our tutorial consists in outputting the Model in a chosen format – in our case Turtle. We will employ the Model method to write with the following signature :

 Model write(OutputStream out, String lang)


Although our statement has been correctly printed out (<; <John Smith> “this is a property”^^<; .), 3 red lines come up in the console (which have been deliberately left in the screenshot) and they refer to an issue concerning Log4J. Jena uses Log4J as a logging system and this error is caused by the fact that the configuration file  cannot be found. It is very likely you will encounter the same issue, so have a look at these resources:

Probably, the easiest way to fix an issue with Log4J is to follow these simple steps:

  • Find a properties file named and copy it;
  • Navigate to your workspace folder where you have your source project;
  • Rename the file to just;
  • Go to bin folder and paste the file there
  • Clean the project and run it again.

The errors should have gone and you should be returned with the following: