File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

How to access EJB from a main method

vn us
Ranch Hand

Joined: Dec 27, 2002
Posts: 33
Hi,
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 ( http://127.0.0.1/helloworld/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());
return;
}
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());
return;
}

Thanks,
AVNUS
Vijayakumar Arya
Ranch Hand

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


Thanks,<p>Vijay<p>The Hand that gives, Gathers.
 
jQuery in Action, 2nd edition
 
subject: How to access EJB from a main method
 
Similar Threads
Entity Bean Lookup Exception
ClassCastException when casting to home interface
jndi nosuchmethoderror
error while running sample EJB
Entity beans could not be used (WSAD 5)