This tutorial will show you how to develop the famous “Hello World” as a HK2 module using NetBeans 6.0/maven.

Before beginning, make sure that you have the IDE's Maven support. You can install the Maven plugin by choosing Tools > Plugins, clicking the Available Plugins tab, selecting the Maven checkbox, and clicking Install.

The very first step is to create a new Maven Project: File:step1_HK2.PNG

as a Maven Quickstart Archetyp: File:step2_HK2.PNG

We also must supply the artifact id, group id and the version. File:step3_HK2.PNG

NetBeans actually creates a Hello world source file named App in the package se.javasolutions.hk2.HelloWorld.

The next thing we need to do is to add the maven repository containing HK2. The pom is located in the Project Files folder. Add the following to pom.xml: File:step4_HK2.PNG

Next we need to add the dependency to the hk2-maven-plugin. To do this, select Add Library on the Libraries folder in the project. Add the following: File:step5_HK2.PNG

We also need to add a dependency to hk2 iteself: File:step6_HK2.PNG

Now we must change the pom file to use the hk2-maven plugin to build and package the the project. File:step7_HK2.PNG

By default NetBeans assumes that we are using Java 1.4 and will therefor not recognize annotations. Select Properties on the project folder and the category source. Change Source/Binary Format to 1.5/1.6. Close and re-open the project.

Now it is time to create the contract for our hello world service: File:step8_HK2.PNG

As you can see is an ordinary interface with an @Contract annotation.

The Service implementation is just as simple: File:step9_HK2.PNG

The App class is modified to implement the interface ModuleStartup. File:step10_HK2.PNG

The service instance is injected to the service field by the HK2 runtime container.

And the very final thing we must to is to modify run project action to (note the Set properties): File:step11_HK2.PNG

Now you can run the project.

The complete NetBeans project with sources can be downloaded here: HK2/

Feel free to send commends to rikard.thulin(at)


  • HK2 development site
  • GlassFish v3 Engineering Guide
  • HK2 presentation by Rikard Thulin & Ferid Sabanovic, IBS JavaSolutions
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo