cancel
Showing results for 
Search instead for 
Did you mean: 

ClassCastException on ejb jndi lookup

Former Member
0 Kudos

Hello,

I try to get an standalone remote client to to connect to my stateless session bean:

java.util.Properties props = new java.util.Properties();

props.put("java.naming.provider.url", "181.205.12.19:50004");

props.put("java.naming.factory.initial", "com.sap.engine.services.jndi.InitialContextFactoryImpl" );

props.put("java.naming.security.authentication", "simple");

props.put("java.naming.security.principal", "j2ee_admin");

props.put("java.naming.security.credentials", "*****");

jndiContext = new InitialContext(props);

fetching of jndi context is fine, but once I try to lookup my home interface I get a class cast exception:

home = (ToEdifactConverterHome) PortableRemoteObject.narrow(jndiContext.lookup ("sap.com/EdifactEar/ToEdifactConverterBean"), ToEdifactConverterHome.class);

java.lang.ClassCastException

at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)

at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)

at test.cceag.transform.TransformTester.setUp(TransformTester.java:74)

at test.cceag.transform.TransformTester.main(TransformTester.java:48)

The jndi lookup seems to return the bean implementation object, but not the home interface itself.

Thanks for any hints

Matthias

Accepted Solutions (0)

Answers (4)

Answers (4)

0 Kudos

The exaception thrown to indicate that your code has attempted to cast an object to a subclass of which it is not an instance. This means that ClassCastException occurs when you try to cast an instance of an Object to a type that it is not. Type Casting only works when the casted object follows an is a relationship to the type you are trying to cast to.

It is good practice to guard any explicit casts with an instanceof check first:

if (myApple instanceof Fruit) { 
Fruit myFruit = (Fruit)myApple; }

When will be ClassCastException is thrown:

  • When you try to cast an object of Parent class to its Child class type, this exception will be thrown.
  • When you try to cast an object of one class into another class type that has not extended the other class or they don't have any relationship between them.
katarzyna_fecht
Explorer
0 Kudos

Hi Matthias,

i think the guide

"How to...EJB: Accessing EJB Applications Using JNDI"

will help you to solve the ejb lookup problem:

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/0736159e-0301-0010-9ea4-c63...

good luck, Katharina

Former Member
0 Kudos

Hi,

If you are using a local lookup, It would help to put a reference (of the bean you are trying to lookup) in the application-j2ee-engine.xml file.

Regards,

Guru

Former Member
0 Kudos

Hi Matthias,

Try with following code for look up:

home = (ToEdifactConverterHome) jndiContext.lookup ("sap.com/EdifactEar/ToEdifactConverterBean");

Regards,

Bhavik