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 How to access EJB from a main method 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 "How to access EJB from a main method" Watch "How to access EJB from a main method" New topic

How to access EJB from a main method

vn us
Ranch Hand

Joined: Dec 27, 2002
Posts: 33
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());

Vijayakumar Arya
Ranch Hand

Joined: Jan 27, 2003
Posts: 76
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

Thanks,<p>Vijay<p>The Hand that gives, Gathers.
I agree. Here's the link:
subject: How to access EJB from a main method
It's not a secret anymore!