Running EJB Client Standalone

By Krishnamoorthy Sethuraman

HelloWorld.java

package myEJB;

import javax.ejb.Remote;

@Remote
public interface HelloWorld {
public String sayHello(String name);
}

HelloWorldBean.java

package myEJB;

import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean implements HelloWorld {
private int count;

public String sayHello(String name) {
count++;
System.out.println(“sayHello method from Bean “);
return “Hello ” + name + ” Count  : ” + count;
}
}

Compile the classes and run in embedded oc4j of jdeveloper. Or create a ejb jar deployment profile and deploy it to standalone oc4j server. Deployment name is the application name in the standalone oc4j. Let us say application is HelloWorldDeploy. Ear file contains jar file and META-INF. Inside META-INF is the application.xml containing the name of ejb module jar file. Jar file contains the interface and bean classes.  After deploying, create a test client as follows:

HelloWorldClient.java

package myEJB;

import java.util.Hashtable;

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

public class HelloWorldClient {
public static void main(String [] args) {
try {
final Context context = new InitialContext();
HelloWorld helloWorld = (HelloWorld)context.lookup(“java:comp/env/ejb/HelloWorldBean”);
// Call any of the Remote methods below to access the EJB
System.out.println(helloWorld.sayHello( “Krishna” ));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

To invoke ejb method from command line:

D:\work\MyWork\ejb\ejb3>set CLASSPATH=D:\installs\JdevStudio10131\j2ee\home\oc4jclient.jar;.\;

D:\work\MyWork\ejb\ejb3>java -Djava.naming.factory.initial=oracle.j2ee.naming.Ap
plicationClientInitialContextFactory -Djava.naming.provider.url=ormi://localhost
:23791/HelloWorldDeploy -Djava.naming.security.principal=oc4jadmin -Djava.naming
.security.credentials=welcome myEJB.HelloWorldClient

Output is : Hello Krishna Count  : 1

3 Responses to “Running EJB Client Standalone”

  1. Ram Says:

    Hi Krishnamoorty,

    I am trying to dependency injection of stateless session bean from a JSF backing bean and I am getting null pointer expception when I try to use the bean.

    Basically SLSB deployed outside this web application is not getting injected.

    Do you have any idea on what I might be missing.

    If you want I can post the source code.

    Thank-ram

  2. Shaban Says:

    I am trying to run a HelloWorld Bean, deployed in J2ee Sdk 1.4. The deployment is 100% successfull but there is thrown one exception when i run the client I got the exception “javax.naming.NameNotFoundException”
    However, i have checked the Jndi name in the application server environment variable, it is the same as im giving in lookup context, why still then my application is not running.

  3. ami Says:

    problem is with ejb-jar.xml

Leave a Reply