javax.naming.Reference cannot be cast to in GlassFish

This error usually happens when the client tries to cast the lookup result to the expected type, such as EJB remote interface. This is what happens behind the scene. During the naming context lookup, a javax.naming.Reference is first acquired, which contains information how to create the actual object for the client. If the object creation step fails, then the half-baked javax.naming.Reference is returned to the client, hence the ClassCastException.

Some reasons why the naming manager failed to create object:

1, the object factory class responsible for creating the object is not available in the client classpath. Think of javax.naming.Reference as the raw material for creating the object. When there is no factory, no object is created, and the raw material is thrown back to the client.

In GlassFish 3, the object factory for remote EJB reference appears to be com.sun.ejb.containers.RemoteBusinessObjectFactory, packaged in ejb-container.jar. So ejb-container.jar and javax.ejb.jar should be in the client classpath by direct inclusion or by reference, along with a series of other jar files.

The following is the toString format of the returned javax.naming.Reference:
Reference Class Name: test.TestIF
Type: url
Content: ejb/TestBean__3_x_Internal_RemoteBusinessHome__
2, the object factory is in client classpath, but not loadable by the current thread context class loader.

The algorithm for creating object is in javax.naming.spi.NamingManager.

Followers

Pageviews Last 7 Days