File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes lookup()    in   EJB3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "lookup()    in   EJB3" Watch "lookup()    in   EJB3" New topic

lookup() in EJB3

Juan Handal

Joined: Jan 25, 2005
Posts: 29

I'm using GlassFish &NetBeans 5.5,no problems at all but

when it comes to a SessionBean(that is the name of the Session Bean) with only one method public String Hello(){return "HelloWorld";}

I tried the standalone client java class accessing public String Hello(){} ,but I can not get the Context.

class Test{

public static void main(String [] args)throws NamingException{
Context ctx=new InitialContext();
SessionBeanLocal hello=SessionBeanLocal)ctx.lookup("ejb/SessionBean");
System.out.println(hello.Hello() );

I got NoContextException ,both the SessionBean ,SessionBeanLocal& class Test are in the same

ejb directory.The problem is the lookup("What is the right JNDI name in here ???")

I'd appreciate any help.


Pradeep Settipalli

Joined: Dec 15, 2006
Posts: 5
i do have a same problem so any one pls suggest me a solution

The Bussiness interface
package hai;

public interface Hello {
public String hello();


The Bean class

package hai;
import javax.ejb.Remote;
import javax.ejb.Stateless;

public class HelloBean implements Hello {
public String hello(){
return "hai";


The client
import javax.naming.*;
public class HelloClient {
public static void main(String[] args) {
Context ctx=new InitialContext();
Hello h=(Hello)ctx.lookup("hai.Hello");
catch(Exception e){


This is the error i get when i try to run

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
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at hai.HelloClient.main(

so pls suggest me a solution for this problem as this is my first ejb program
Thanks in advance
Joe Khan

Joined: Aug 27, 2004
Posts: 15
Make two changes:

(1)In the Bean class:

(2)In the client code:
Context ctx=new InitialContext();
Hello h=(Hello)ctx.lookup("XYZ");
[ May 21, 2007: Message edited by: Joe Khan ]

All things are difficult before they are easy...
Oliver Kamps

Joined: May 15, 2007
Posts: 18

I haven't used glassfish, so the following might be off the mark.

I assume that you are running to run your code as stand-alone clients of that EJB.

Looking at the exceptions, I'm not convinced that the JNDI name is the problem: it seems to me that JNDI doesn't know what initial context factory to use. Check out this GlassFish FAQ for more details.

alex Rozario

Joined: May 15, 2007
Posts: 4
hi juan & pradeep,

me too have an same error.
just now i posted that.

hi mr.joe
i have tried your suggestion
but still it shows me the same error
can you help me further
thank you
Dave Michels

Joined: Mar 29, 2007
Posts: 5

It apprears that your client doesn't have the correct IntialContextFactory configured for the JNDI environment based on the exception stack trace. See the following Glassfish Naming Factory Env

and see if that helps.
Juan Handal

Joined: Jan 25, 2005
Posts: 29
Hi Dave

You are right.I'm trying to get the right one setProperty:

Properties props = new Properties();

// optional. Defaults to localhost. Only needed if web server is running
// on a different host than the appserver
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");

// optional. Defaults to 3700. Only needed if target orb port is not 3700.
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

InitialContext ic = new InitialContext(props);

Where can I get the appservrt.jar's jndi to look the right property?

I'm using netbeans 5.5 & GlassFish

Thanks a lot.

I agree. Here's the link:
subject: lookup() in EJB3
It's not a secret anymore!