IBM Support

ClassCastException when casting a remote EJB home or remote interface stub instance

Troubleshooting


Problem

You receive a ClassCastException when casting a remote Enterprise Java™Beans home or remote interface stub instance to its actual remote home interface or remote interface type. When resolving a remote EJB home reference or a local EJB home reference, an application programmer may "cleverly" attempt to narrow the result of a JNDI lookup using the EJBHome class rather than the exact home class (type) in order to generalize the task. Here is an example involving a remote Enterprise JavaBeans, MyBean and it's corresponding home, MyHome: [] InitialContext context = new InitialContext(); Object remoteObj = context.lookup("java:comp/env/ejb/MyHomeName"); EJBHome ejbHome = PortableRemoteObject.narrow(remoteObj, EJBHome.class): MyBean myBean = (MyBean)ejbHome.create(); ... []

Symptom

In this example, a ClassCastException indicating class MyBean_Stubxxx occurs when the application casts the result of myHome.create() to class MyBean. The explanation is provided below.

A ClassCastException occurs if myHome (an instance of EJBHome, above) is cast to its actual type, MyHome. The following example statements also result in a ClassCastException indicating the MyHome_Stubxxx class:

MyHome myHome = (MyHome)ejbHome;

Which is synonymous with:

MyHome myHome = (MyHome)PortableRemoteObject.narrow(object, EJBHome.class):

Because the code snippets above violate the Enterprise JavaBeans 2.0 specification with regard to narrowing remote EJB references, their expected behavior is indeterminate.

[{"Product":{"code":"SSEQTP","label":"WebSphere Application Server"},"Business Unit":{"code":"BU048","label":"IBM Software"},"Component":"General","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF010","label":"HP-UX"},{"code":"PF016","label":"Linux"},{"code":"PF027","label":"Solaris"},{"code":"PF033","label":"Windows"}],"Version":"9.0;8.5.5;8.0;7.0","Edition":"Base;Network Deployment","Line of Business":{"code":"LOB77","label":"Automation Platform"}},{"Product":{"code":"SSNVBF","label":"Runtimes for Java Technology"},"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Component":"Java SDK","Platform":[{"code":"","label":""}],"Version":"","Edition":"","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

To view more of this document

This document has the abstract of a technical article that is available to authorized users once you have logged on. Please use the link to actual document below to access the full document. You will be asked to log on if you are not already logged in. After log in, if you do not have the right authorization for this document, there will be instructions on what to do next.

Document Information

More support for:
WebSphere Application Server

Software version:
9.0, 8.5.5, 8.0, 7.0

Operating system(s):
AIX, HP-UX, Linux, Solaris, Windows

Document number:
81059

Modified date:
03 March 2025

UID

swg21192568

Manage My Notification Subscriptions