• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to access EJB from a main method

Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me with some suggestions
I'm using Sun one ( version7 ) app server, This application has some samples and I'm trying to execute those samples ( Stateless session bean simple bean example )
I'm able to access the EJB through servlet ( GreeterServlet ( )
I'm trying to access the same EJB through a standalone java class through JNDI lookup
I'm getting the following error :0
Greeter bean home not found - Is bean registered with JNDI?: 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

Here is my Javaclass code, Interestingly this is the same code that is there in the servlet, I'm missing the some where with the JNDI property, this is available in the servlet container env. but not in my standalone javaclass
Java main method code:
Greeter myGreeterBean;
GreeterHome myGreeterHome;
Greeter myGreeterRemote;
InitialContext initContext = null;
try {
initContext = new javax.naming.InitialContext();
catch (Exception e) {
System.out.println("Exception creating InitialContext: " + e.toString());
try {
System.out.println("Looking up greeter bean home interface");
String JNDIName = "java:comp/env/ejb/greeter"; System.out.println("Looking up: " + JNDIName);
Object objref = initContext.lookup(JNDIName);
myGreeterHome = (GreeterHome)PortableRemoteObject.narrow(objref, GreeterHome.class);
catch(Exception e) {
System.out.println("Greeter bean home not found - " +"Is bean registered with JNDI?: " + e.toString());

Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you are trying to access the EJB from a standalone client you have to provide the following,
1. give the name of the class that will provides InitialContextFactory implementation - please refer to the Sun One documentation regarding this.
2. provider URL - need to specify the URL to access the JNDI registry of the Sun One Server.
3. include the home interface and remote interface of the EJBs in the class path so that your class can load them and use it.
Though this does not provide all the details expected, but tries to throw some light on the problem. I am not familiar with Sun One server.
These things should be followed whether you are using weblogic or Sun One or any other J2EE app server.
Hope this helps
Seriously Rick? Seriously? You might as well just read this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic