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 ClassCastException while invoking PortableRemoteObject.narrow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "ClassCastException while invoking PortableRemoteObject.narrow" Watch "ClassCastException while invoking PortableRemoteObject.narrow" New topic

ClassCastException while invoking PortableRemoteObject.narrow

Sara Lee

Joined: Sep 13, 2005
Posts: 12
I am using Eclipse and JBoss to build a simple J2EE application (EJB and Servlets).However I get a ClassCastException in the init method of the servlet when the following code is invoked:-

Context context = new InitialContext();
Object ref = context.lookup("java:/comp/env/ejb/Cart");
%***I get the ClassCastException when the underlying code is executed**%
home = (CartHome) PortableRemoteObject.narrow(ref, CartHome.class);
CartHome cart = home.create();


When PortableRemoteObject.narrow is called the value of the variable ref is ejb/CartHome.

jboss.xml contains the following element:-

I am just beginning with EJB development and am not sure how to proceed. Any help will be welcome. Thanks in advance!

SCJP 1.4, SCWCD 1.4
Chris Brat
Ranch Hand

Joined: May 22, 2003
Posts: 108
Hi Sara,

I dont see much of a problem with the PortableRemoteObject.narrow.... code.

There is a problem with the Home interface's create method returning a Home interface object - it should return an object that returns the component interface (i.e. remote interface).

Sara Lee

Joined: Sep 13, 2005
Posts: 12
Hi Chris,
Sorry.. I had mistyped when posting my message. The create method does return a remote interface object. The narrow method throws a class cast exception before the create method is called. I am reposting my code again :-

public class CartServlet extends HttpServlet {
private CartHome home; //CartHome is the home interface
private Cart cart; // Cart is the remote interface
public void init(ServletConfig config)throws ServletException{
Context context = new InitialContext();
Object ref = context.lookup("java:/comp/env/ejb/Cart");
//***Code fails with a class cast exception below
home = (CartHome) PortableRemoteObject.narrow(ref, CartHome.class);
//** As a result cart is never initialized
cart = home.create();
}catch(Exception e){
System.out.println("Exception caught in init: "+e);

I have a feeling, it may be some simple configuration issue...but am not sure what else I need to do. I have used the following website as a reference

I would appreciate any suggestions that anyone may have.

Thanks in advance!
Sara Lee

Joined: Sep 13, 2005
Posts: 12
Hi All,
I found the source of the problem to be the inclusion of the ejb classes in my war file as required bu the tutorial I had used as a reference. I removed the ejb classes from the packaging configuration of the war file and it started working. I wanted to share the resolution with everyone.

I agree. Here's the link:
subject: ClassCastException while invoking PortableRemoteObject.narrow
It's not a secret anymore!