This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes EJB and other Java EE Technologies and the fly likes Got ClassCastException with EJB3, please help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Got ClassCastException with EJB3, please help!" Watch "Got ClassCastException with EJB3, please help!" New topic
Author

Got ClassCastException with EJB3, please help!

Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
Hi Friends,

I am writing my first ejb 3 project that has a simple stateless session bean, which is accessed by my struts action class. Here is how the bean class looks like:



However, I got ClassCastException in my action class when trying to cast the bean to its local/remote interface type:



or for remote interface:



I am using JBoss Eclipse IDE 1.6 and running JBoss AS 4.0.5. The EJB 3 code is packaged into ejb-jar.jar and the web app is in its war file, then both the jar and the war are packed into an ear file.

Can someone please help me figure out what's wrong here?

Thanks,
Tong
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Try casting it to the Bean class instead of casting to an interface.

Also, with Remote lookups you do not need to use PortableRemoteObject.narrow

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10059
    
163

Originally posted by Mark Spritzler:
Also, with Remote lookups you do not need to use PortableRemoteObject.narrow


I guess, you meant with local lookups, we dont require the PortableRemoteObject.narrow call.

However, I got ClassCastException in my action class when trying to cast the bean to its local/remote interface type


Have a look at ClassCastExceptions which will help you debug classcastexceptions on JBoss. Specifically, look at the JMX console method mentioned over there.


[My Blog] [JavaRanch Journal]
Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
Thanks for both replies.

I tried the jmx-console method displayClassInfo for both interface types and the bean class type. And none of them showed any indication of duplicate class loading.

So, anything other suggestions? Or, I guess I should ask what is the proper way of packaging ejb and web app. Right now, I pack ejb remote/local interface types and bean class types into ejb-jar; and pack ejb remote/local interface types and web app files into the war file.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I guess, you meant with local lookups, we dont require the PortableRemoteObject.narrow call.


He is using EJB 3.0 so I meant for remote you do not need PortableRemoteObject.narrow calls anymore.

Mark
Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
Hi Mark,

I tried casting it directly to the remote interface without using PortableRemoteObject.narrow(), but I still got the ClassCastException. Also, I am not sure what you mean by "Try casting it to the Bean class instead of casting to an interface.", my understanding is you only expose ejb interfaces (remote or local) to the client, instead of the actual bean class. Anyway, I also tried that, but still the same thing.

Can you point me to a working sample EAR that has a version 2.4 web app with a ejb 3 module?

Thanks,
Tong
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10059
    
163

Originally posted by Mark Spritzler:

He is using EJB 3.0 so I meant for remote you do not need PortableRemoteObject.narrow calls anymore.

Mark


Sorry, i overlooked that point.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10059
    
163

Originally posted by Tong Wang:
I tried casting it directly to the remote interface without using PortableRemoteObject.narrow(), but I still got the ClassCastException.


Can you post the exception stacktrace? Also what's the output when you add the following System.out.println statement afer the lookup

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

What does your application.xml file look like. Can you post that?

Yes, you should only be casting to the interface, I just wanted to see if it was something else. Anyway, you application.xml needs to have modules that point to each archive file. And you definitely have the interfaces in the ejb's jar file?

Mark
Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
I added the line to display the class type of the ref object from jndi and below is the output:

Object is of type: class $Proxy79

My application.xml looks like this:

Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
The exception looks like this:

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Try this for your remote lookup

BeanRemote bl = (BeanRemote) jndiContext.lookup("myapp/BeanRemote");

You do not need to do a narrow in EJB3.

Mark
Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
I got the same exception even without using PortableRemoteObject.narrow and doing the casting directly. The jndi name seems to be fine, because I can see it in the jmx-console's JNDIView.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by Tong Wang:
I got the same exception even without using PortableRemoteObject.narrow and doing the casting directly. The jndi name seems to be fine, because I can see it in the jmx-console's JNDIView.


Great, that shows that you don't need to use narrow. Forget about narrow.

Now the problem is classpath, for some reason you interfaces are not in the client's classpath.

Mark
Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
I packed the interface classes into the WAR and interface and bean classes into the ejb-jar. I don't have any <ejb-ref> in my web.xml, since it's not needed for ejb3. Am I doing anything wrong that could affect the classpath?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10059
    
163

I packed the interface classes into the WAR and interface and bean classes into the ejb-jar


There lies the problem. You have placed the interface(s) in more than one package. Remove the interface(s) from the war and let them remain in the ejb-jar. That should solve it. The JMX console method mentioned in one of the links that i posted earlier, should have detected this.
Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
Removing the interface classes from the WAR solved the problem. Thanks a lot for the help.

By the way, if I want to use entity beans, how should I package them?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Got ClassCastException with EJB3, please help!