I am trying to access an ejb method sayHello() from my client code.
What I have did:
1:created a HelloWorld interface
package remoteif;
import java.rmi.*;
import javax.ejb.*;
public interface HelloWorld extends EJBObject{
public String sayHello() throws RemoteException;
}
2:HelloWorldHome
package homeif;
import remoteif.HelloWorld;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;
public interface HelloWorldHome extends EJBHome {
public HelloWorld create() throws
CreateException, RemoteException;
}
3: a Bean Implementation HelloWorldBean
package beanimpl;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.rmi.RemoteException;
public class HelloWorldBean implements SessionBean {
// Methods of Remote interface
public String sayHello() {
return "Hello, world !";
}
// Methods of Home interface
public void ejbCreate() {}
protected SessionContext ctx;
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
}
@Override
public void ejbRemove() throws EJBException, RemoteException {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void ejbActivate() throws EJBException, RemoteException {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void ejbPassivate() throws EJBException, RemoteException {
//To change body of implemented methods use File | Settings | File Templates.
}
}
4:Deployment descriptors : jonas-ejb-jar.xml & ejb-jar.xml
5:created jar files by including these classes ,interfaces and the xml files
6:created a client application & added the created jar as library file
7.created HelloClient
package client;
import homeif.HelloWorldHome;
import remoteif.HelloWorld;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class HelloClient {
public static void main(String args[]) {
try {
Context initialContext = new InitialContext();
Object objref = initialContext.lookup("myHelloWorld");
System.out.println("3");
HelloWorldHome home =
(HelloWorldHome) PortableRemoteObject.narrow(objref,
HelloWorldHome.class);
HelloWorld myHelloWorld = home.create();
String message = myHelloWorld.sayHello();
System.out.println(message);
} catch (Exception e) {
System.err.println(" Error : " + e);
System.exit(2);
}
}
}
But while running it, I am getting
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.initialHow can I solve this?