This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes inconvertible types attempting to cast ejb JNDI lookup to home interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "inconvertible types attempting to cast ejb JNDI lookup to home interface" Watch "inconvertible types attempting to cast ejb JNDI lookup to home interface" New topic
Author

inconvertible types attempting to cast ejb JNDI lookup to home interface

John Davis
Ranch Hand

Joined: Nov 02, 2000
Posts: 189
I receive the following when trying to compile a servlet (a servlet which attempts to obtain a home interface via a JNDI lookup)
C:\ASAservletCode\ASAdonations.java:61: inconvertible types
found : java.lang.Object
required: RagHome
RagHomehome= (RagHome) context.lookup( "Rag" );
^
1 error
Finished
The code in the servlet is:
Properties env = new Properties();

env.put( "java.naming.factory.initial",
"desisoft.ejb.client.JRMPFactory" );

env.put( "desisoft.ejb.nameServer1",
"g2h:2050" );

Context context = new InitialContext( env );

RagHomehome= (RagHome) context.lookup( "Rag" );
The compiler obviously knows about RagHome. The context.lookup method returns a Java.lang.object
I am attempting a cast of object to RagHome.
What am I doing wrong?
Shin Hashitani
Ranch Hand

Joined: Dec 04, 2001
Posts: 54
Hi John,
Try this:
...
import javax.rmi.PortableRemoteObject;
...
java.lang.Object obj = Objectcontext.lookup( "Rag" );
RagHome home = (RagHome) PortableRemoteObject.narrow(obj, RagHome.class);

It is required to support RMI-IIOP for remote access in EJB. Since CORBA supports a lot of languages, and some of them don't support casting, you need to explicitly narrow the object coming back from lookup().
When you use lookup() and the reference is remote, you always need narrow(). Or, if you need to get remote reference by using Handle, HomeHandle,or EJBMetaData, you need narrow().
There might be other situations that you need narrow() method, but I am not sure.
I hope this helps.

Shin Hashitani


/*<br /> * Quants Inc.<br /> * Engineer<br /> *<br /> * Shin Hashitani<br /> * hashitani@quants.co.jp<br /> * <a href="http://www.quants.co.jp" target="_blank" rel="nofollow">www.quants.co.jp</a><br />*/
Shin Hashitani
Ranch Hand

Joined: Dec 04, 2001
Posts: 54
There is another situation:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=11&t=002370
When getting a reference contained in Collection or Enumeration resulting from a finder method, you need narrow().
Joe McGuire
Ranch Hand

Joined: Mar 19, 2001
Posts: 293
While in development you may get an inconvertible types error, such as the code:

Returns the error:
ClientTest.java:30: inconvertible types
found : java.lang.Object
required: CabinHome
CabinHome home = (CabinHome) PortableRemoteObject.narrow(obj, CabinHome.class);
^

I have found that explicitly importing the Home interface resolves the issue:

--------------------
Joe McGuire
Sun Certified Java™ 2 Programmer, BEA WLS Certified Developer
[ March 07, 2002: Message edited by: Joe McGuire ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: inconvertible types attempting to cast ejb JNDI lookup to home interface
 
Similar Threads
inconvertible types attempting to cast ejb JNDI lookup to home interface
ClassCastException returned from PortableRemoteObject.narrow
Question about accessing EJB components in OC4J
JNDI lookup problem
WebSphere 5