Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "ClassCastException while invoking PortableRemoteObject.narrow" Watch "ClassCastException while invoking PortableRemoteObject.narrow" New topic
Author

ClassCastException while invoking PortableRemoteObject.narrow

Sara Lee
Greenhorn

Joined: Sep 13, 2005
Posts: 12
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!


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).


SCJP 1.2, SCJP 5, SCBCD
Sara Lee
Greenhorn

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{
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

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.

Regards,
Sara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException while invoking PortableRemoteObject.narrow
 
Similar Threads
Topic: access remote session ejb on remote server
Problem: creating EJBs in Client in Weblogic7.0
home.create()=>failed : java.lang. NoClassDefFoundError
Not able to excute EJB ClassCastException
Context Lookup Failing