wood burning stoves 2.0*
The moose likes JBoss/WildFly and the fly likes Running EJB 3.0 Session Bean in JBoss Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "Running EJB 3.0 Session Bean in JBoss" Watch "Running EJB 3.0 Session Bean in JBoss" New topic

Running EJB 3.0 Session Bean in JBoss

Reema Patel
Ranch Hand

Joined: Jan 26, 2006
Posts: 169
I�m facing problems in running a simple Session bean example based on EJB 3.0 spec. For better understand, I have enumerated the following points:-

1.I have installed JBoss AS 4 with EJB support
2.I have set the environment variables (CLASSPATH, JBOSS_HOME etc) appropriately for the required JAR files.
3.Following are the source code files-
package beans;

import javax.ejb.Remote;

public interface SimpleSession
public String getEchoString(String clientString);

package beans;

import javax.ejb.Stateless;

public class SimpleSessionBean implements SimpleSession {
public String getEchoString(String clientString) {
return clientString + " - from session bean";

package client;

import beans.SimpleSession;
import javax.naming.InitialContext;

public class SimpleSessionClient {
public static void main(String[] args) throws Exception
InitialContext ctx = new InitialContext();
SimpleSession simpleSession
= (SimpleSession) ctx.lookup(SimpleSession.class.getName());
for (int i = 0; i < args.length; i++) {
String returnedString = simpleSession.getEchoString(args[i]);
System.out.println("sent string: " + args[i] +", received string: " + returnedString);

4.This is how I have compiled my code
javac -d . client/*.java
javac -d . beans/*.java

5.And, this is how I have created the ejb3 JAR file
jar cf SimpleSessionApp.ejb3 beans\*.java

6.For deployment, I have copied the JAR file created in step 5 inside the %JBOSS_HOME%/server/all/deploy directory
7.Finally, I�m running the client as shown below:
org.jboss.naming rg.jnp.interfaces
localhost client.SimpleSessionClient
Now is the time for all good men

But, unfortunately I�m getting the following run-time error
Exception in thread "main" javax.naming.NameNotFoundException: beans.SimpleSession not bound

Can anybody pls let me know the cause with suggestions to recover from the errors.

In anticipation

Jagmohan Negi

Joined: Aug 24, 2005
Posts: 27
Check if the EJB is deployed or not.
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385

I am facing the same problem. Have been trying this for past 2 days. What is the solution.

SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 9921

Please provide more details about your application and the exception that you are seeing.

[My Blog] [JavaRanch Journal]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249


It does not require you to have such an environment variable, but you do need a JAVA_HOME, but you already have that too, otherwise you wouldn't have the app server started.

Your problem is the lookup string you are using. You are getting using the className, which isn't how JBoss binds it in the JNDI tree.

I don't recall off the top of my head what that string is, something I try not to clog my brain with. Jaikiran will know it for sure.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 9921

I don't recall off the top of my head what that string is, something I try not to clog my brain with. Jaikiran will know it for sure.

For EJB3, by default, the jndi name is a based on the combination of ApplicationName/BeanImplementationClassName/local or ApplicationName/BeanImplementationClassName/remote.

For example, if the application is MyApp and the bean implementation class is org.myapp.ejb.SimpleSession and if you are doing a lookup of remote interface then the default jndi name would be MyApp/SimpleSession/remote
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I get the following error when I changed the JNDI name

javax.naming.NameNotFoundException: Remote 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:296)
at org.jnp.server.NamingServer.lookup(NamingServer.java:270)
at org.jnp.server.NamingServer.lookup(NamingServer.java:270)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.webage.client.TestClient.runTest(TestClient.java:17)
at com.webage.client.TestClient.main(TestClient.java:25)
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I am again iterating the steps which I have done to create ejb

1. I created a package com.webage.ejbs
i. Created an Interface

package com.webage.ejbs;
import javax.ejb.*;

public interface SimpleBean {
public String sayHello(String name);

ii. Create the class for implementing the interface

package com.webage.ejbs;

import javax.ejb.Stateless;

public class SimpleBeanImpl implements SimpleBean {
public String sayHello(String name) {
return "Hello " + name + "!";

2. I Packed the above class in beans.jar

3. I created a folder and copied the beans.jar in it.

4. I also created a META-INF Folder & copied a application.xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" version="1.4"
xsi:schemaLocation="http://java.sun.com /xml/ns/j2ee [url=http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">]http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">[/url]
<description>Simple EJB3</description>



5. I named the folder as firstebj3.ear

6. Final Folder Structure

7. I copied the firstebj3.ear in <JSOSS_HOME>\server\default\deploy folder

8. I started the server.

9. Following is the snap shot of http://localhost:8080/jmx-console

Searched For: jboss.j2ee

10. I am trying to call the above ejb from local client

package com.webage.client;

import java.util.Properties;

import javax.naming.*;
import com.webage.ejbs.SimpleBean;

public class TestClient {

public void runTest() throws Exception {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming rg.jnp.interfaces");
props.setProperty("java.naming.provider.url", "jnp://");

InitialContext ctx = new InitialContext(props);
SimpleBean bean = (SimpleBean) ctx.lookup("firstebj3/SimpleBeanImpl/Remote");
String result = bean.sayHello("Billy Bob");

public static void main(String[] args) {
try {
TestClient cli = new TestClient();

} catch (Exception e) {


Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I changed the JNDI lookup name to firstebj3/SimpleBeanImpl

I got this name from jmx console.!!

I guess now the client is able to do the look up. Now I get error saying that..

java.lang.ClassCastException: org.jnp.interfaces.NamingContext
at com.webage.client.TestClient.runTest(TestClient.java:21)
at com.webage.client.TestClient.main(TestClient.java:29)

Any suggestions!!
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 9921

I guess, you are still using the wrong jndi name for the lookup. You mentioned, you used firstebj3/SimpleBeanImpl/Remote to do the lookup. Change it to:

Note, the lowercase 'r' in Remote. Give it a try.
vinay kumar gundu

Joined: Mar 26, 2007
Posts: 5
Give jndi name like this



it will work fine...
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Yups...it worked with ctx.lookup("firstebj3/SimpleBeanImpl/remote");

Hemkiran Gadey

Joined: Aug 29, 2005
Posts: 2

Could you post the contents of your ejb-jar.xml?

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385

There is no ejbjar.xml. ejb-jar.xml is not mandatory with Ejb3.0
subject: Running EJB 3.0 Session Bean in JBoss
Similar Threads
NoInitialContextException error
Stuck In the Mud with First EJB
Connection to remote Glassfish server
Deploying an EJB error
Bean Not Bound Exception