Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

ClassCastException in EJB lookup


I developed an EJB in Netweaver and it is running fine together with my WebDynPro. Now i'm using the EJB as library file in my another Java standalone program that running using eclipse. I import it as External JAR into my application. When i try to run it, it will hit java.lang.ClassCastException. Here is my code.

try {
      Properties props = new Properties();
      props.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "");
      props.put(javax.naming.Context.PROVIDER_URL, "localhost:50000");
      Context ctx = new InitialContext(props);
      TestHome home = (TestHome)ctx.lookup("SENDER");
      test = home.create();

I got the error java.lang.ClassCastException error when i try to run it and the problem is in the TestHome home = (TestHome)ctx.lookup("SENDER");

I tried to change the code to

TestHome home = (TestHome)javax.rmi.PortableRemoteObject.narrow(ctx.lookup("SENDER"), TestHome.class);

And also this way

Object obj = ctx.lookup("SENDER");
TestHome home = (TestHome)javax.rmi.PortableRemoteObject.narrow(obj, TestHome.class);

And i got this error which i think is the similar as both way is different syntax but same function type.

	at javax.rmi.PortableRemoteObject.narrow(
	at net.model.proxy.NewProxyClient$
	at java.util.TimerThread.mainLoop(

I tried to google and check out SDN. Some said bout the classloader that cause this. I go through this document [How To ... EJB: Accessing EJB Applications using JNDI|] and follow it, but still having the problem ClassCastException. Any help for this? Appreciate for the helps. Thanks..


I tried to create a new Session Bean in my EJB application. Test it out, to my surprise it able to connect and create in my Java program. The session bean is empty. Then i tried to create a new Session bean with another name and JNDI, then copy the code into the new session bean. The same problem is occurs. So which mean some problem with my session bean. Will try to figure it out.

Edited by: Adrian Chan on May 5, 2009 12:32 PM

Former Member

Helpful Answer

Not what you were looking for? View more on this topic or Ask a question