Hibernate and Jersey Conflict on Glassfish

Last weekend I tried to use Jersey in an enterprise application to make some RESTful web services available. The idea was to generate data to be plot by flash charts. So, I created a new web module in addition to the already existent one. I thought it was better to keep it in a separate module to avoid changing the original web application, since it still uses Struts as web framework. Before adding any RESTful web service, I just created the web application and deployed it successfully as an additional enterprise module. I’m happy about that because I will use the same strategy to migrate the web module from Struts to JSF, keeping the Struts version working in parallel to the JSF version, both sharing the same business logic.

I use Netbeans because it makes my life easier. The Jersey module is available in the 6.1 version and for download in the update center for previous versions. Since installed, you may click with the right button on a web project to see the context menu and select “New … / RESTful Web Services from Patterns” or “New … / RESTful Web Services from Entity classes”. Since I’m not implementing entity classes in this module, I selected the one from patterns. It will start a wizard to prepare the application for REST and generate the initial class skeleton to start working.

I followed those steps and the RESTful web service was created successfully. However, when I start to deploy the whole application again, an obscure error appears in the Glassfish output log insistently. In such situation I tried everything: clean, build, clean and build, remove the module and deploy it separately, but nothing was enough to solve the problem below:

LifecycleException: java.lang.NoSuchMethodError: org.objectweb.asm.ClassReader.accept(Lorg/objectweb/asm/ClassVisitor;I)
(…)
Caused by: java.lang.IllegalStateException:
ContainerBase.addChild:
start: LifecycleException:
java.lang.NoSuchMethodError: org.objectweb.asm.ClassReader.accept(Lorg/objectweb/asm/ClassVisitor;I)

I checked if I was using the libraries appropriately, if they were correctly wrapped and if there was any library conflict. Yes, there is a library conflict with Hibernate, the persistence framework I am using as a JPA implementation. Jersey uses the ASM library, which is also used by Hibernate, but on different versions. Hibernate uses version 1.5.3 and Jersey uses 3.5.1. A big difference! We can not just delete the old version and put the new one there because they are architecturally different. Version 1.5.3 has an “attrs” package while 3.5.1 doesn’t have it but a “signatures” package. Changing the version can cause other problems and I’m not expecting them afterwards. So, how do I solve it consistently?

The ASM package is needed by the cglib package, which is part of the Hibernate libraries. If we remove that package, Jersey will work correctly, but Hibernate will stop working. To solve this conflict use cglib-nodep.jar instead of cglib.jar and keep ASM version 3.x with Jersey. cglib-nodep.jar includes some ASM classes demanded by cglib.jar, changing the package name to avoid any class conflict.

The ASM library is a Java bytecode manipulation and analysis framework. According to its website “(…) it can be used to modify existing classes or dynamically generate classes, directly in binary form. Provides common transformation and analysis algorithms to easily assemble custom complex transformations and code analysis tools“. ASM is used by many products like AspectJ, Oracle TopLink, JRuby, and many others. It can not be simply ignored by frameworks because it is a matter of flexibility. The best alternative is always to investigate the unexpected problem and claim for a better error presentation on the JVM.

42 Responses to “Hibernate and Jersey Conflict on Glassfish”

  1. hiltond says:

    Thanks for this post, really helped me. For no reason I can think of, Netbeans6.1 decided to add jersey to my web app, causing all the errors you stated. I just did an upgrade so maybe that triggered it, who knows, either ways, big thanks !!

  2. Mike says:

    You should try using TopLink for your JPA implementation. Even though TopLink uses ASM, it won’t conflict with other layers that use different versions of it.

  3. Hi Mike! Actually, I started using Toplink Essentials at the beginning, but when errors occur they are not so clear like the ones generated by Hibernate. I moved to Hibernate also because Toplink is not in the long term plans of the Glassfish team, since they are adopting Eclipse Link instead of Toplink on Glassfish V3.

    Toplink Essentials is, actually, not so good like the one sold by Oracle. I think it was just a marketing strategy from Oracle.

  4. Sebastian says:

    Thank you very much for this help, i solved my problem this way.

    ;)

  5. natt says:

    I had the same issue. resolved by using cglib-nodep. thanks much for posting this solution.

  6. Anonymous says:

    Same problem with Netbeans 6.5.

    Thanks much for this solution

  7. Anonymous says:

    Hello –

    I too faced the same issue when using hibernate/jersey/jboss. What I did was download asm-3.0.jar file and put it in the jboss lib folder. and then restart jboss. and it solved the issue !

  8. Anonymous says:

    Hello!
    very good solution. But there is a problem that any jsp or html page is not dispaying in the application where i had replaced the cglib and asm jars. The error http 404 is encountered when i tried to load any jsp or html. When I replaced back the orignal versions of cglib and asm, the html and jsp are visible correctly but jersey stops working. I am using tomcat 6.x server

  9. Hello Anonymous,

    Usually when this error happens you probably had a problem to deploy your application. HTTP 404 means nothing available on the server for the requested URL. So, your problem could be well described in the log files of Tomcat. Only with this logs is possible to figure out what is going on there.

  10. Anonymous says:

    I had the same Issue . resolved by using the cglib-nodep-2.2.jar. Thanks for posting this issue

  11. guido says:

    Had the same, greetings from Luxembourg

  12. Richard Knuszka says:

    Good article and fixed worked for me too. Thanks for taking the time to document this.

  13. Jeyendran Balakrishnan says:

    Awesome!
    I ran into the same problem with NetBeans 6.5, where I was using Hibernate JPA as well as Jersey.
    I just replaced cglib-2.1.3.jar [in the Hibernate distribution bundled with NetBeans] with cglib-nodep-2.2.jar], and it got rid of all my asm run-time exceptions.

    Thanks for the solution!

  14. Anwar says:

    It helped
    Thanks much

  15. Kevin says:

    Fantastic! Worked with the app I had created with NetBeans 6.7.1. I removed the asm, asm-attr and cgilib jars from the Hibernate JTA group of libraries, and manually added cgilib-nodep which I downloaded from sourceforge. Everything is now working on Tomcat 6. Thanks for posting this.

  16. Augustine says:

    while using the cglib-nodep-2.2.jar and JpaDaoSupport in spring , I am getting a new exception as java.lang.ClassCastException: $Proxy29 cannot be cast to. But this jar works fine for jpaTemplate.

  17. Andrew Velis says:

    Thanks for the detailed information on the conflict and a solution to resolve it. Your solution worked like a charm. I hope one day Jersey and Hibernate can play nice but for now this works.

  18. Harsh says:

    Thanks a ton !! This was really useful. It was like a nightmare resolved and that too just before my project deadlines.

  19. Wendy says:

    Hey !
    I really like to thank you for this solution.
    You saved me a lot of time !!!
    Thanks again !!!

  20. Lives2Sail says:

    Thanks so much! This resolved my problem after much head banging

  21. Dan says:

    Adding to the love fest from Costa Rica. Thanks, for the nice, clean solution. Peace.

  22. Anonymous says:

    Thanks a lot for this information. It helped me debug my problem. Users facing this problem should also check out the solution provided on:

    http://forum.springsource.org/showthread.php?t=26713

    This link has some good script to resolve this problem for maven 2 users.

  23. Anonymous says:

    Thank you very much!!!

  24. Developer says:

    I know you have heard it before but thanks again

  25. Anonymous says:

    One more time: Thank You!!

  26. Anonymous says:

    You saved my day ! Thank you very much !

    Matthieu from France

  27. Anonymous says:

    Many many many thanks!!
    Philippe

  28. Anonymous says:

    Thx for your help. That is what I need to continiou with my job

  29. Thanks for you dedication to bring this priceless information to us! I was strugling with my Maven + Hibernate + Jersey long time. =)

    Best Regards

    Paulo Miguel Almeida

  30. Anonymous says:

    Thank you for a Belgian for your help ;).
    I use Netbeans 7.0. I have removed the file /hibernate/cglib-2.1.3.jar. I have found /cglib-2.2 that I’have renamed by /cglib-nodep-2.2.jar and have added.

    John[BE]

  31. Maybe we can have a beer some day because I’m also hosted in Belgium ;)

  32. Thank you! You’ve just made my day!

  33. dr@g0n says:

    Thanks so much. Really was looking for the solution for a very long time.

  34. Anonymous says:

    Thanks, this solved my problem

  35. Xuan Dung says:

    Thanks a lot. 2.30am! My problem was solved. @@

  36. Anonymous says:

    You are a hero, thank you very much!!!! :D

  37. Anshul says:

    Thanks

    Really works!!

  38. Anonymous says:

    Thanks for your solution! It works like a charm.

  39. Doru says:

    Thank you very much for solving my problem!
    I used cglib-nodep-2.2.2.jar and works just fine.

  40. Prakash says:

    Thank you! it solved my problem, was struggling with Hibernate and Jersey.
    I replaced cglib-2.1.3.jar by cglib-nodep-2.1_3.jar

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>