Hello World in Kompics

This section describes the simplest possible Kompics program, the well-known Hello World.

A proper introduction can be found in the Kompics Tutorial.

Maven Setup

Set up a new maven project and follow the instructions in Repositories and Getting Started to include the necessary dependencies. For this simple example the kompics-core module will be sufficient.

The resulting pom.xml might look something like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

        Kompics Hello World



            <name>SICS Release Repository</name>
            <name>SICS Snapshot Repository</name>

Hello World Component

This very simple component contains but a single handler, which is executed when a Start event is triggered on the control port. Start events are part of Kompic’s component lifecycle and always need to be triggered on a component exactly once to get it to handle events. A component that is never started is considered paused and will queue up events on its ports, but never execute them.

package se.sics.test;

import se.sics.kompics.ComponentDefinition;
import se.sics.kompics.Handler;
import se.sics.kompics.Start;
import se.sics.kompics.Kompics;

public class HelloComponent extends ComponentDefinition {
		Handler<Start> startHandler = new Handler<Start>() {
			public void handle(Start event) {
				System.out.println("Hello World!");
		subscribe(startHandler, control);


Do not forget to subscribe Kompics handlers to ports! It is the most common mistake, and it is very easy to overlook when debugging.


The Main.java is very simple and only starts the Kompics framework by calling Kompics.createAndStart with the class of the root component. It also automatically sends a Start event to the component once the constructor has finished.

package se.sics.test;

import se.sics.kompics.Kompics;

public class Main {
	public static void main(String[] args) {

Compiling and Running

To compile the source code use:

mvn clean compile

To run the project from within maven:

mvn exec:java -Dexec.mainClass="se.sics.test.Main"

This is all. Say hello to Kompics ;)


You might notice that if you used only the code from above log4j complains that is improperly configured. To fix that you can add a simple src/main/resources/log4j.properties file to the project to configure the logging system:

# C1 is a console appender
log4j.appender.C1.layout.ConversionPattern=%d{[HH:mm:ss,SSS]} %-5p {%c{1}} %m%n

log4j.rootLogger=DEBUG, C1


If you run it again the log4j warnings should have disappeared.


You can download the whole maven project here.