• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Got ClassCastException with EJB3, please help!

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Tong Wang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exception looks like this:

 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic