1
votes

I created a bean object and encountered this error at run time,

java.lang.ClassCastException at 
com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(Unknown Source) at 
javax.rmi.PortableRemoteObject.narrow(Unknown Source) at Client_TestPortal.main Client_TestPortal.java:71) Caused by: 
java.lang.ClassCastException: javax.naming.Reference ... 3 more 

How to create bean object in my client? I have, a bean Interface, TestPortal, and a bean class, TestPortalBean, in an ear, PortalEJB.

Here is the code, I am using to create EJB instance in client,

String sEjbRemote = "PortalEJB/TestPortalBean/remote";

Properties pProp = new Properties();
pProp.put("java.naming.factory.initial",sInitCtxtCls);
pProp.put("java.naming.provider.url", sUrl);

javax.naming.InitialContext initialContext = new InitialContext(pProp);
Object ref = initialContext.lookup(sEjbRemote);
System.out.println("\n\n \t Source :::"+ref.toString());

test.ejb.TestPortal testportal = (test.ejb.TestPortal)PortableRemoteObject.narrow(ref,test.ejb.TestPortal.class);

Object ref = initialContext.lookup(sEjbRemote);

When i print the object in SOP ref.toString();

i got the following information but i am not able to create object for TestPortal which is in the PoratlEJB.ear which is deployed in JBOSS- AS version : Jboss-5.0.1.GA

     Source :::Reference Class Name: Proxy for: test.ejb.TestPortal

Type: ProxyFactoryKey Content: ProxyFactory/TestPortalBean/PortalEJB/TestPortalBean/remote Type: EJB Container Name Content: jboss.j2ee:ear=PortalEJB.ear,jar=PortalEJB.jar,name=TestPortalBean,serv ice=EJB3 Type: Proxy Factory is Local Content: false Type: Remote Business Interface Content: test.ejb.TestPortal Type: Remoting Host URL Content: socket://s9458:3973/

1
How are you creating the object in client? Show some code here.Adeel Ansari
What are you using? J2ee 1.4? Java EE 5/6? Web interface? Remote rich client?Puce

1 Answers

1
votes

Typically, we do something like this,

Properties props = getConfigurationProps();
InitialContext ctx = new InitialContext(props);
tp = (TestPortal) ctx.lookup(TEST_PORTAL_JNDI_NAME);

[Edited]

From your code I can see that you are trying to narrow() the object. Lets see what documentation is saying regarding this, it says,


Checks to ensure that an object of a remote or abstract interface type can be cast to a desired type. 

Parameters:
    narrowFrom - the object to check.
    narrowTo - the desired type. 
Returns:
    an object which can be cast to the desired type. 
Throws: 
    ClassCastException - if narrowFrom cannot be cast to narrowTo.

I am not sure what are you up to. But you can always do this, in the first place, as I have already shown in my first attempt.

TestPortal ref = (TestPortal) initialContext.lookup(sEjbRemote);

instead of,

Object ref = initialContext.lookup(sEjbRemote);