Thursday, October 8, 2015

A Simple Enterprise JavaBeans 3.1 Example with WildFly 9 (and Eclipse)

(This message is an update from the original one, written on October 15, 2012, for JBoss 7. I now use WildFly 9.)

We will do a very simple Enterprise JavaBeans example: a stateless bean that will add two integer numbers. We will use Eclipse, but NetBeans is not so different.

First, you must pick the Java EE perspective on the right upper corner:

Next, you must create a new Java EJB project (File--> New --> EJB Project --- check the picture after this one) and select the Finish button in the bottom:

To successfully create a project you may need to create a New WildFly Runtime, something that may not be available before you pick the option "Show additional server adapters":

Next, you may create a new Session Bean, as follows (note the package ejbs):

You get the following window. Pick the appropriate options and press "Finish":

We now can write the remote interface:
package ejbs;
import javax.ejb.Remote;

public interface AddBeanRemote {
public int add(int a, int b);

and the source code of the bean, which implements the previous interface:
package ejbs;

import javax.ejb.Stateless;

 * Session Bean implementation class AddBean
public class AddBean implements AddBeanRemote {

    public AddBean() { }

    public int add(int a, int b) {
    return a + b;

Now, it's time to deploy. We will run the server on a console and export the .jar file. Running everything inside Eclipse is also an option. To export the jar file to deploy the project, you may use the right button of the mouse and do as in the following figures:

Once you press "Finish" you should see the following on the JBoss console. If you didn't yet start JBoss you can do it by running the ./ (or .bat) script file on the bin directory:

You should also see the following result in the deployment directory:

To undeploy this project, you may just delete the .deployed file  from this directory.

Now, let's get back to the names WildFly displays on the console. We will need these names in our client application. For this application, we will use just another standard Java project. For that, you may even change the perspective back to Java and then you need to create a Java Project:

and a Java class:

After you click finish, we need to configure the build path:

To include the EJB project and a jar file called "jboss-client.jar". The former contains the Java definitions of the bean that we must use. The latter contains the classes that allow the client to use JNDI to get the stub of the EJB. To add the project, pick the "Projects" tab:

Now, "Add External JARs..." in the Libraries tab. The jar is in the bin/client folder of WildFly. Its name is "jboss-client.jar":

The following piece of code is the main class:

import javax.naming.InitialContext;
import javax.naming.NamingException;

import ejbs.AddBeanRemote;

public class PlayAddBean {
* @param args
* @throws NamingException 
public static void main(String[] args) throws NamingException {
AddBeanRemote addbean = (AddBeanRemote) InitialContext.doLookup("EJB-Add/AddBean!ejbs.AddBeanRemote");
System.out.println("3 + 2 = " + addbean.add(3, 2));
System.out.println("Very impressive!");

We also need to add a new file called "" to the source folder of the project (use the right button over the "src" folder):

This is the file. Note that you should change the username and password to match your own case. This file sets the behavior of the InitialContext object we create in the main() method. E.g., it will tell how to create the JNDI client, where is the server (machine and port), etc.:


As we run the client, we get the following result:


  1. Simple and easily understandable... Thank you Very Much!

  2. Why my 'import ejbs.AddBeanRemote' show error, I have add jar jboss-client.jar

    1. It's probably due to the lack of the the EJB-Add project in the java build path (it is in one of the pictures above).

  3. where src folder. i do not see that to create the jndi client with below code


  4. The src folder is the folder where the packages with the source code are. In Eclipse you should usually see it on the left side of the screen, in the Project Explorer tab.

  5. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative training in chennai | chennai's no.1 java training in chennai

  6. perfect explanation about java programming .its very useful.thanks for your valuable training in chennai | java training in velachery

  7. Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information
    Very informative ..i suggest this blog to my friends..Thank you for sharingjava training in chennai | java training institutes in chennai