Looking for a JavaEE Architect or Potential Tech Co-Founder?
Please don't hesitate to contact me.

Creating your first EJB3 Project in eclipse on JBoss Server

This tutorial will teach you on how to setup and run an ejb application.

What you need (noted are where I installed my versions):
1.) Jboss 5 (jboss-5.1.0.GA)
2.) eclipse-jee
3.) ojdbc14 (C:\jboss-5.1.0.GA\server\default\lib)

1.) Create a new EJB project.
  a.) In your eclipse select File->New->Other->EJB->EJB Project. I named mine ipielEJB
b.) Click next c.) Click next d.) Check Generate ejb-jar.xml, then click Finish
2.) Add a persistence.xml in your project (inside META-INF folder) (See file below) 3.) Create a new package com.kbs 4.) Create a new class Book.java annotate as Entity (See file below) 5.) Create 2 interface BookBeanLocal and BookBeanRemote, annotate with Local and Remote (See files below) 6.) Create a new class BookBean that implements BookBeanLocal and BookBeanRemote. Annotate with Stateless. (See file below) 7.) Create ipielEJB-ds.xml. For format look here: C:\jboss-5.1.0.GA\docs\examples\jca 8.) Our ejb project is almost finish by now. 9.) Configure your jboss server
10.) Deploy ipielEJB to jboss (See screenshot) 11.) You will know if the deployment is successful if you got no error on Console log and can see the following log.

Now to test the EJB bean.
1.) In the same project create a package com.kbs.test
2.) In the newly create package create a class BookClient (See class below)
3.) In ejbModule folder create a new file jndi.properties (See file below)
4.) Run the client. The below screen shot is how the project should look like. Also the Book run log can be seen.

package com.kbs;

import java.io.Serializable;

import javax.persistence.*;

@Table(name = "book")
@SequenceGenerator(name = "book_sequence", sequenceName = "book_id_seq")
public class Book implements Serializable {
 private static final long serialVersionUID = 7422574264557894633L;
 private Integer id;
 private String title;
 private String author;

 public Book() {

 public Book(Integer id, String title, String author) {
  this.id = id;
  this.title = title;
  this.author = author;

 public String toString() {
  return "Book: " + getId() + " Title " + getTitle() + " Author "
    + getAuthor();

 public String getAuthor() {
  return author;

 public void setAuthor(String author) {
  this.author = author;

 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_sequence")
 public Integer getId() {
  return id;

 public void setId(Integer id) {
  this.id = id;

 public String getTitle() {
  return title;

 public void setTitle(String title) {
  this.title = title;
package com.kbs;

import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class BookBean implements BookBeanLocal, BookBeanRemote {
 EntityManager em;
 public static final String RemoteJNDIName = BookBean.class
   .getSimpleName() + "/remote";
 public static final String LocalJNDIName = BookBean.class
   .getSimpleName() + "/local";

 public void test() {
  Book book = new Book(null, "Hello Java Bean I!", "Edward");
  Book book2 = new Book(null, "Hello Java Bean II", "czetsuya");
  Book book3 = new Book(null, "Hello Java Bean III",
  System.out.println("list some books");
  List someBooks = em.createQuery("from Book b where b.author=:name")
    .setParameter("name", "czetsuya").getResultList();
  for (Iterator iter = someBooks.iterator(); iter.hasNext();) {
   Book element = (Book) iter.next();
  System.out.println("List all books");
  List allBooks = em.createQuery("from Book").getResultList();
  for (Iterator iter = allBooks.iterator(); iter.hasNext();) {
   Book element = (Book) iter.next();
  System.out.println("delete a book");
  System.out.println("List all books");
  allBooks = em.createQuery("from Book").getResultList();
  for (Iterator iter = allBooks.iterator(); iter.hasNext();) {
   Book element = (Book) iter.next();
package com.kbs;

import javax.ejb.Local;

public interface BookBeanLocal {
 void test();
package com.kbs;

import javax.ejb.Remote;

public interface BookBeanRemote {
 void test();
package com.kbs.test;

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

import com.kbs.BookBean;
import com.kbs.BookBeanRemote;

public class BookClient {
 public static void main(String[] args) {
  Context context;
  try {
   context = new InitialContext();
   BookBeanRemote beanRemote = (BookBeanRemote) context
  } catch (NamingException e) {
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
Manifest-Version: 1.0
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
 <persistence-unit name="ipielEJB">
   <property name="hibernate.hbm2ddl.auto" value="create-drop" />

Commonly encoutered errors:

1.) Deployment "jboss.jca:name=ejb3ProjectDS,service=DataSourceBinding" is in error due to the following reason(s): ** NOT FOUND Depends on 'jboss.jca:name=ejb3ProjectDS,service=DataSourceBinding' **
Deployment "<UNKNOWN jboss.j2ee:jar=FirstEjb3Tutorial.jar,name=BookTestBean,service=EJB3>" is in error due to the following reason(s): ** UNRESOLVED Demands 'persistence.unit:unitName=#FirstEjb3Tutorial' **
-Simply means your ojdbc14.jar is not where it should be on C:\jboss-5.1.0.GA\server\default\lib

2.) Failed to resolve schema nsURI= location=persistence
-persistence.xml is missing version, xmlns, etc tag

3.) EJBException unknown entity
-Book entity (Book.java) is not properly configured

4.) javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
-Missing or unconfigured jndi.properties
Creating your first EJB3 Project in eclipse on JBoss Server Creating your first EJB3 Project in eclipse on JBoss Server Reviewed by Edward Legaspi on Wednesday, March 02, 2011 Rating: 5


Anonymous said...

This is a great post!!! Thanks!!!

Pardhasaradhi said...

Very good post.. Address almost all the queries and the common problems too. Good job.....

Anonymous said...

Thank you very much for your post. I followed all of your steps, however I still encounter the 4th error: javax.naming.NoInitialContextException. I used Jboss6 and Eclipse SDK 3.6.1 + Java EE plugin. Please help me with this error. Thanks in advance

Anonymous said...

i am using jboss 5.1 and trying to deploy ejb3 module in er with war file.. but i am faicng issues " failed to find container meta data for ejb interface com.xxx.xxx.xx.TestBean"

pls advise

Anonymous said...

the tutorial is very good, Easy to understand. i faced some problem with sequence generator because i am using SQLServer but i was able to resolve it. below are the changes done

in persistence.xml replaced

in book.java
@TableGenerator(name="TABLE_GEN", table="SEQUENCE_TABLE", pkColumnName="SEQ_NAME",
valueColumnName="SEQ_COUNT", pkColumnValue="EMP_SEQ")

instead of @SequenceGenerator(name = "book_sequence", sequenceName = "book_id_seq")


@GeneratedValue(strategy=GenerationType.TABLE, generator="TABLE_GEN")

instead of @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_sequence")

this resolved my problem but since i changed the dialect i have to create the tables manually

Viji said...

on exectuting BookClient, i get the following error:

javax.naming.NameNotFoundException: BookBean not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at com.kbs.test.BookClient.main(BookClient.java:16)

Please help.

Maddy said...

I also have the same error like user Vji...
Kindly help out..

Viji said...

I solved the error. In persistence.xml, i changed the data source to DefaultDS as shown below:

Powered by Blogger.