Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException while invoking PortableRemoteObject.narrow

 
Sara Lee
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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:-
<session>
<ejb-name>Cart</ejb-name>
<jndi-name>ejb/Cart</jndi-name>
</session>

I am just beginning with EJB development and am not sure how to proceed. Any help will be welcome. Thanks in advance!
 
Chris Brat
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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{
try
{
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 http://docs.jboss.com/jbosside/tutorial/build/en/html/index.html

I would appreciate any suggestions that anyone may have.

Thanks in advance!
 
Sara Lee
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Regards,
Sara
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic