{"id":66,"date":"2014-04-02T10:50:18","date_gmt":"2014-04-02T09:50:18","guid":{"rendered":"http:\/\/lexxweb.co.uk\/blog\/?p=66"},"modified":"2014-11-07T14:25:47","modified_gmt":"2014-11-07T14:25:47","slug":"nasty-class-loader-issue-java-lang-linkageerror-loader-constraint-violation-loader-previously-initiated-loading-for-a-different-type","status":"publish","type":"post","link":"https:\/\/blog.lexxweb.co.uk\/?p=66","title":{"rendered":"Nasty Class loader issue: &#8220;java.lang.LinkageError: loader constraint violation: loader previously initiated loading for a different type&#8230;&#8221;"},"content":{"rendered":"<h2>Background<\/h2>\n<p>First a bit of background, I am working as part of a team responsible for building a bespoke content management system for <a href=\"https:\/\/isaacphysics.org\" target=\"_blank\">educational uses<\/a> and the back end uses the following technologies:<\/p>\n<ul>\n<li>Java Tomcat 7.0 server<\/li>\n<li><a href=\"http:\/\/www.jboss.org\/resteasy\">Rest Easy<\/a>\u00a03.0.6.Final<\/li>\n<li><a href=\"http:\/\/www.elasticsearch.org\/overview\/\">ElasticSearch <\/a>1.0.1<\/li>\n<li><a href=\"https:\/\/code.google.com\/p\/google-guice\/\">Google Guice<\/a> 3.0 for dependency injection<\/li>\n<li>Eclipse Kepler Build 20130919-0819<\/li>\n<li><a href=\"https:\/\/code.google.com\/p\/counterclockwise\/\">Counterclockwise <\/a>(Clojure plugin For Eclipse) 0.20.0 &amp; 0.24.1.STABLE001 &#8211; This becomes important later even though I am not using it really.<\/li>\n<\/ul>\n<h2>The problem<\/h2>\n<p>Now to describe the problem. For some reason after integrating <a href=\"http:\/\/www.elasticsearch.org\/overview\/\">ElasticSearch <\/a>with our project a nasty error as seen below started happening (and breaking the whole site at runtime when running within eclipse):<\/p>\n<p><span style=\"color: #993300;\"><code>SEVERE: Servlet.service() for servlet [jsp] in context with path [\/rutherford-server] threw exception [Filter execution threw an exception] with root cause<br \/>\njava.lang.LinkageError: loader constraint violation: loader (instance of org\/apache\/catalina\/loader\/WebappClassLoader) previously initiated loading for a different type with name \"javax\/servlet\/http\/HttpSession\"<\/code><\/span><\/p>\n<p>This problem seemed to refer at a random place in our code in the stack trace. This random place had previously worked (and is totally unrelated to the Elastic Search stuff I was working on) in my case it occurred in a User Manager class that deals with sessions (hence the reference to HttpSession). The elastic search stuff didn&#8217;t have anything to do with the user manager or even use the HttpSession class as far as I was aware but seemed to trigger the error when the code for the elasticsearch java api was inserted.<br \/>\n<!--more--><\/p>\n<h2>Investigating<\/h2>\n<p>A really useful explanation of how class loaders work can be found in\u00a0<a href=\"http:\/\/frankkieviet.blogspot.co.uk\/2009\/03\/javalanglinkageerror-loader-constraint.html\">Frank Kieviet&#8217;s blog<\/a>; it certainly helped me understand the strange behaviour i.e. random things breaking that are totally unrelated to the change being made.<\/p>\n<p>As mentioned in Frank Kieviet&#8217;s blog, this type of error is extremely difficult to debug and it appears that it is related to the sequence in which classes are loaded into the jvm.<\/p>\n<p>My approach, after initially (and wrongly) \u00a0blaming Google Guice and ElasticSearch, eventually lead me to investigate what was actually asking for another version of the HttpSession class. In order to do this I added the following to my run configuration for the tomcat server within the eclipse run configuration.<\/p>\n<p><code>-verbose:class<\/code><\/p>\n<div id=\"attachment_69\" style=\"width: 160px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/lexxweb.co.uk\/blog\/wp-content\/uploads\/2014\/04\/clojureBugRunConf.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-69\" class=\"size-thumbnail wp-image-69\" title=\"Eclipse tomcat runtime configuration\" alt=\"clojureBugRunConf\" src=\"http:\/\/lexxweb.co.uk\/blog\/wp-content\/uploads\/2014\/04\/clojureBugRunConf-150x150.png\" width=\"150\" height=\"150\" \/><\/a><p id=\"caption-attachment-69\" class=\"wp-caption-text\">Demonstrating the additional debug parameter to use<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>Naturally, this caused a huge torrent of information to be spewed out of the console screen; so much so that I had to make the console buffer set to unlimited size to preserve all of the information.<\/p>\n<p>After some targeted searching of the output I found the following two lines (in different areas of the output):<\/p>\n<ol>\n<li><code><\/code><code>[Loaded javax.servlet.http.HttpSession from file:\/C:\/Program%20Files\/Apache%20Software%20Foundation\/Tomcat%207.0\/lib\/servlet-api.jar]<\/code><\/li>\n<li><code>[Loaded javax.servlet.http.HttpSession from file:\/C:\/eclipse\/plugins\/ccw.core_0.20.0.STABLE001\/lib\/servlet-api.jar]<\/code><\/li>\n<\/ol>\n<p>Strange I hear you all say. Why would there be an eclipse plugin displayed in the console of my tomcat run environment. Well that is what I thought too. After some more investigation the offending plugin is Counter Clockwise, a cojure plugin for eclipse.<\/p>\n<h2>Conclusion<\/h2>\n<p>After uninstalling the clojure plugin everything was fine again; elastic search and the user manager worked like a dream. I did try updating from Clojure plugin version 0.20.0 to 0.24.1 but everything still broke.<\/p>\n<p>I have a few outstanding questions like:<\/p>\n<ul>\n<li>Why are my eclipse plugins interfering with my tomcat environment running in eclipse (I thought it would run in a different JVM)?<\/li>\n<li>Why is clojure running all the time anyway (I wasn&#8217;t even using the plugin for anything I was doing at the time)?<\/li>\n<li>Shouldn&#8217;t the tomcat instance running within eclipse be sandboxed from plugins etc.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> I will try and come up with a simple example that breaks consistently and post it here but in the mean time if anyone is interested I can set you up with a copy of the code base at the point we were having the errors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Background First a bit of background, I am working as part of a team responsible for building a bespoke content management system for educational uses and the back end uses the following technologies: Java Tomcat 7.0 server Rest Easy\u00a03.0.6.Final ElasticSearch 1.0.1 Google Guice 3.0 for dependency injection Eclipse Kepler Build 20130919-0819 Counterclockwise (Clojure plugin For [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5,3,7,4],"tags":[23,13,24,25,14],"class_list":["post-66","post","type-post","status-publish","format-standard","hentry","category-eclipseplugins","category-general","category-java","category-programming","tag-clojure","tag-eclipse","tag-error","tag-java","tag-plugin"],"_links":{"self":[{"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=66"}],"version-history":[{"count":9,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions\/83"}],"wp:attachment":[{"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lexxweb.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}