At first I want to tell that similar type of questions has been asked here but still I can't find the solution of my problem.
I have a EJB 3 Project (name = HelloWorldEJBProject). There I have created a stateless remote EJB (name = HelloWorldEJB). I have also created an remote interface there (name= HelloWorldEJBInterfaceRemote). After that I am creating a jar & an ear of the project after compiling everything using ant. Then I deployed the EAR in the WebSphere Application Server 6.1.
Next I have also created a stand alone java project (name = HelloWorldClient), put the jar of the HelloWorldEJBProject to this project build path. Now while I am doing the look up I am getting errors.
HelloWorldEJB.java:
package com.staples.ejb;
import com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote;
import javax.ejb.Stateless;
@Stateless
public class HelloWorldEJB implements HelloWorldEJBInterfaceRemote {
public HelloWorldEJB() {
}
public String helloWorld() {
return "Hello World";
}
}
Client.java (Inside HelloWorldClient project):
package com.staples.client.processor;
import com.staples.client.util.ApplicationUtil;
import com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote;
public class Client {
static private HelloWorldEJBInterfaceRemote helloInterface = ApplicationUtil.getHelloEJBHandle();
public static void main(String[] args) {
System.out.println(helloInterface.helloWorld());
}
}
ApplicationUtil.java (Inside HelloWorldClient project):
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote;
public class ApplicationUtil {
public static Object getContext(){
Object obj = null;
Context context;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL,"corbaloc:iiop:localhost:2811");
Context ctx = new InitialContext(env);
obj = ctx.lookup("ejb/HelloEAR/Hello.jar/HelloWorldEJB#" + HelloWorldEJBInterfaceRemote.class.getName());
} catch (NamingException e) {
e.printStackTrace();
}
return obj;
}
public static HelloWorldEJBInterfaceRemote getHelloEJBHandle()
{
Object obj = getContext();
HelloWorldEJBInterfaceRemote helloInterface = (HelloWorldEJBInterfaceRemote) PortableRemoteObject.narrow(obj, HelloWorldEJBInterfaceRemote.class);
//HelloWorldEJBInterfaceRemote helloInterface = (HelloWorldEJBInterfaceRemote) obj;
return helloInterface;
}
Error:
Exception in thread "P=141210:O=0:CT" java.lang.ClassCastException: Unable to load class: com.staples.ejb.interfaces._HelloWorldEJBInterfaceRemote_Stub
at com.ibm.rmi.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:372)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:156)
at com.staples.client.util.ApplicationUtil.getHelloEJBHandle(ApplicationUtil.java:41)
at com.staples.client.processor.Client.main(Client.java:14)
If I comment the following
HelloWorldEJBInterfaceRemote helloInterface = (HelloWorldEJBInterfaceRemote) PortableRemoteObject.narrow(obj, HelloWorldEJBInterfaceRemote.class);
& uncomment the following:
HelloWorldEJBInterfaceRemote helloInterface = (HelloWorldEJBInterfaceRemote) obj;
then the following error comes:
Exception in thread "P=346416:O=0:CT" java.lang.ClassCastException: org.omg.stub.java.rmi._Remote_Stub incompatible with com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote
at com.staples.client.util.ApplicationUtil.getHelloEJBHandle(ApplicationUtil.java:42)
at com.staples.client.processor.Client.main(Client.java:14)
I am using EJB 3 so I guess I dont need the PortableRemoteObject.narrow but without using that I get different error. I am not sure also what to write as a argument of context.lookup(). I am pretty new in EJB. Can anybody help me please? Thanks in advance. Thanks to MagicWand & bkail to solve my port & jndi problems.
After you have started getting
NamingException
, usingcom.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote
as yourJNDI
lookup string should work. Have you tried using that too?