Home‎ > ‎JBoss‎ > ‎JBoss Application Server‎ > ‎

EJB3 Session bean tutorial using JBoss and Eclipse

This "Hello World" tutorial shows how to develop deploy and test EJB3 Session bean in JBoss server and write a simple client and test it.

Application Server - JBosss 4.2.2 GA
IDE - Eclipse 3.4.1

File => New => Project => EJB Project

Give project a name and select JBoss as Target Run time. (Assume that you have already configured JBoss in Eclipse) and select finish.

Crete following Interfaces and classes.


import java.io.Serializable;
public interface IMyBean extends Serializable {   
    public void doSomething();


import javax.ejb.Remote;
public interface MyBeanRemote extends IMyBean{


import javax.ejb.Local;
public interface MyBeanLocal extends IMyBean {


import javax.ejb.Stateless;
public class MyBean implements MyBeanLocal, MyBeanRemote {
    private static final long serialVersionUID = 9184424076718418234L;
    public void doSomething() {
        System.out.println("Hello World!");        

Now select Menu option: Run As => Run on Server option to deploy the EJB project in JBoss server.

This will result in ProjectName.jar file in deploy directory in JBoss. i.e  jboss-4.2.2.GA\server\default\deploy

Now lets create a Java EE client project.

Select New => Java EE => Application Client Project

Give new project a name and select JBoss as target run time and select Finish.

Now add jboss-4.2.2.GA\server\default\deploy\ProjectName.jar into the class path of your client project.

Now create new Java class as follows;


import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MyBeanClient {
    public static void main(String[] args) {
        try {
            Properties props = new Properties();
            props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
            props.setProperty("java.naming.provider.url", "");        
            InitialContext ctx = new InitialContext(props);           
            MyBeanRemote bean = (MyBeanRemote) ctx.lookup("MyBean/remote");
        } catch (NamingException e) {

Make sure to change the value for "java.naming.provider.ur" to reflect your server name and port if necessary.

Now run your MyBeanClient class as a normal Java class.

You will get an out put like following in the console:

07:58:36,156 INFO  [STDOUT] Hello World!

Congratulations you have develop deploy and test your first EJB3 session bean using Jboss server and Eclipse IDE.

Reference projects are attacched.
- Session Bean project
- Client project

- JBoss Application Server Documentation - Chapter 1. Enterprise Applications with EJB3 Services
- Jboss EJB3 Tutorial
- Jboss EJB3 Tutorial Sample - Stateless Session Bean
- First EJB 3 Tutorial showing a session and entity beans with annotations and JBoss.

n g,
Jun 3, 2009, 7:32 AM
n g,
Jun 3, 2009, 7:32 AM