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:

<foaf:PersonalProfileDocument rdf:about="">
<foaf:maker rdf:resource="#me"/>
<foaf:primaryTopic rdf:resource="#me"/>
<admin:generatorAgent rdf:resource=""/>
<foaf:Person rdf:ID="me">
<foaf:name>Ilaria C</foaf:name>
<foaf:homepage rdf:resource=""/>

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:

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:


Humanities Majors and the corporate world: the great fit

Semi-serious account on how valuable Humanities Majors are and should be in nowadays economy. A praise of sophism and cogent reasoning during office meetings and our daily decision making processes in business. An enjoyable reading and a self-affirmation journey for all Humanities graduates who were repetitively asked “So, what do you want to do when you graduate?”

Link to the original article:

Geospatial Search Engine at the Library of Congress

Log4j: a brief introduction

What is Log4j? 

Log4j is a logging Java Framework, which offers a more efficient and modular alternative to System.out.println. It is an Apache Software Foundation Project, which in turn is part of bigger initiative called Apache Logging. The current version is now Log4j 2.0, which has superseded Log4j 1.x.

Why Log4j? 

Log4j controls the logging behavior through the editing of a configuration file at the runtime and without modifying the application binary. One of its distinctive features is the notion of inheritance of loggers. In a nutshell,  by employing a ‘log hierarchy’ it is possible to control which log statements are outputted according to the notion of  ‘logging level’.

There are 6 levels of logging in Log4j:

  • INFO
  • WARN

The ability of selecting the logging level helps reduce the volume of logged output and make life easier when inspecting logs.

The target of the log output can be a file, an OutputStream, a, a remote Log4j server, a remote Unix Syslog daemon, etc.

 What do I need to know first and foremost? 

There are 3 main concepts underlying Log4j,  as follows :


Loggers: Loggers are basic, developer customized, org.apache.log4j.Logger objects, which provide control mechanism over disabling or enabling certain type of log statements when logged against them. Each class in your application can have an individual logger or a common logger. Log4j provides a root logger where all loggers (including custom ones) will inherit from. This also means that it is not strictly necessary to have an individual logger for each class as you can always use the root logger by calling Logger.getRootLogger(). Nevertheless, it is not recommended to use the root logger.

As mentioned already, we can set the level of each logger, according to the set of logging levels. The root logger always has a default level assigned to it, which is DEBUG.

Appenders: Appenders define the properties of a Logger, meaning that they set the target or the destination of a specific logger. For example, the Default root level Logger has  a default Console Appender associated with it, thus it prints to the Console by default.

Layouts: Layouts define the style and content of the output log. Some built-in layouts are provided with log4j, although it is also possible create your own layouts, if required. For example, a layout can specify the date and time or include information about the line number from which the log originates.


Relevant links and resources:


Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: