Show TOC Start of Content Area

Background documentation ClassCastException Occurs After Lookup Operation   Locate the document in its SAP Library structure

Symptom

A lookup operation of an object from a server-side client (application, service, library) returns the expected object type, but when trying to cast it to a proper interface, a ClassCastException occurs.

Scenario Type: Error analysis

SAP NetWeaver Component: Application Server Java (AS Java)

Syntax

Example:

Context jndiCtx = new InitialContext();

Object object =  jndiCtx.lookup(“objectName”);                       LookedUpObjectInterface result = (LookedUpObjectInterface)  PortableRemoteObject.narrow (object, LookedUpObjectInterface.class);

 

Throws:

java.lang.ClassCastException: <LookedUpObjectImpl>

      at

com.sap.engine.services.cross.PortableRemoteObjectContainer.narrow(PortableRemoteObjectContainer.java:154)

      at

com.sap.engine.system.PortableRemoteObjectProxy.narrow(PortableRemoteObjectProxy.java:24) ...............

 

Analysis

Usually, the client does not have a class loader reference to the component that has bound the looked-up object. However, in the current example the client has put the class, which has to be cast (LookedUpObjectInterface in the example) in its package.

In most cases, applications have sapj2eeclient.jar in their lib folder. sapj2eeclient.jar is only for remote client use. When your source is running on the server side, you have to specify references to all the resources you need. For example, to look up a TableLocking instance, you need a reference to service applocking. The problem appears when a class file is located in two different JAR files. The TableLocking interface is both in applocking.jar and in sapj2eeclient.jar, so the class loaders are different and the class cast exception occurs.

The purpose of the references is that a specific class file has to be found only in one place in all the server-side sources.

Solution

      Check if the class file which has to be cast to (LookedUpObjectInterface in the above example) and any classes used by this class are in the client component package (the application EAR file) and remove them.

      Put a class loader reference to the component, which has bound the object you are trying to look up.

 

End of Content Area