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 EJB Local Home Interface ClassCastException!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB Local Home Interface ClassCastException!!" Watch "EJB Local Home Interface ClassCastException!!" New topic

EJB Local Home Interface ClassCastException!!

kenshin Lin

Joined: Dec 16, 2003
Posts: 5
Hi all:
i encounter a problem that a ClassCastException occured when i lookup
a EJBLocalHome object, and then cast it. i can sure the object
of "lookup" is implemented by Container. Why this happened?
my code is:
Context ctx = new InitialContext();

Object obj = ctx.lookup("DBSessionLocal");
if(obj instanceof DBSessionLocalHome){
System.out.println("not ok");
the output is "not ok"

and if(obj instanceof EJBLocalHome) is "true" , some one can tell me


Sergiu Truta
Ranch Hand

Joined: Dec 16, 2003
Posts: 121
Try this:
Object ref = ctx.lookup(jndiName);
EJBHome value = (EJBHome)PortableRemoteObject.narrow(ref,
where the "className" is the fully qualified name, like:
Enjoy I'm walking the path...
Srikanth Shenoy
Ranch Hand

Joined: Jan 24, 2004
Posts: 184
Hi Kenshin,
Sergiu is correct. You have to use PortableRemoteObject to narrow the JNDI returned object.
If you are still getting ClasscastException after following Sergiu's tip, then I presume your class packaging itself might be to blame.
This happens if one your class loaded by EAR class loader is trying to acess the EJB. For more details on this, please refer to the free chapters from "J2EE Project Survival Guide" available for public review at
Server Side. It covers Class Loading in J2EE in detail.
Srikanth Shenoy
Struts Survival Guide - Basics to Best Practices
J2EE Project Survival Guide

Srikanth Shenoy
Author of Struts Survival Guide : Basics to Best Practices
kenshin Lin

Joined: Dec 16, 2003
Posts: 5
hi :
but the object i lookup is "Local Home object" , i think that there is
no need to cast the object by using PortableRemoteObject.narrow()
ps: the clinet is in the same JVM
Ken Boyd
Ranch Hand

Joined: Dec 10, 2003
Posts: 329
We still have to cast because the return type of lookup is Object, but we don't have to narrow it since it isn't a stub i.e. client is local. If you have Head First EJB refer to page 160.
Hope this help.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: EJB Local Home Interface ClassCastException!!
Similar Threads
ArrayList of Different Datatypes!!
determining an object class is an array, list etc
Integer[] instanceof Object[]?
Doubt in instanceof operator