aspose file tools*
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 Spring in Action this week in the Spring 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
Author

EJB Local Home Interface ClassCastException!!

kenshin Lin
Greenhorn

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("ok");
}else{
System.out.println("not ok");
}
=================================================
the output is "not ok"

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

why?


SCJP SCWCD SCBCD
Sergiu Truta
Ranch Hand

Joined: Dec 16, 2003
Posts: 121
Try this:
Object ref = ctx.lookup(jndiName);
try
{
EJBHome value = (EJBHome)PortableRemoteObject.narrow(ref,
Class.forName(className));
......
}
where the "className" is the fully qualified name, like:
com.pack1.pack2.....packn.EJBHome
Enjoy


...watch me...as I'm walking the path...
Srikanth Shenoy
author
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.
Thanks,
Srikanth Shenoy
Author
Struts Survival Guide - Basics to Best Practices
J2EE Project Survival Guide


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

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.


SCJP, SCWCD, SCBCD, SCJD, BB Java2 and JSP1.1
 
wood burning stoves
 
subject: EJB Local Home Interface ClassCastException!!