In this blog post we use JHades to troubleshoot problems in our classpath. JHades is a powerful tool that gives us useful information when trying to resolve a certain set of problems in Java development.
The demo behind this blog post builds on the following: RAML code generation.
It has nothing to do with RAML per se. I’ll just be using and continuing from the demo introduced in that blog post (note that each post’s “resulting” project state is in its own branch).
Okay, so before we can get to the meat of things, we need a problem to troubleshoot.
Go ahead and git clone https://github.com/ricston-git/blog-contacts-app && cd blog-contacts-app && git checkout raml-code-generation (this is the demo behind the previous post). Now, cd into contacts-app and mvn install. Cd into contacts-app-impl and mvn exec:java -Dexec.mainClass="com.ricston.contacts.Main". If your JAVA_HOME env variable is pointing to a Java 6 installation you’ll get: Unsupported major.minor version 51.0. This is because I upgraded Jersey to 2.13 – so you’ll want to point it to your Java 7 home directory. You should then see Server listening on port 4433 – indicating that our server is running and we can hit it with requests as defined in our RAML file.
So… no trouble – app works fine. That’s because you’re using the fixed version 🙂
What you need to do to follow along is open up contacts-app-api’s pom.xml and remove the scope of provided on the raml-jaxrs-codegen-core dependency. That (and a couple of other unnecessary things) was the state of the project when I started working on the demo code behind the blog post which I was going to write originally (then I ended up writing this one). What actually happened was that I was able to continue working without facing this problem for quite a while because I was running the program from within my IDE. I had set up another Maven module with war packaging, transferred the configuration from the Main.java over to a web.xml deployment descriptor and fired things up with no problem in my IDE. It was only when I tried running the project from Maven using the Tomcat plugin – mvn clean tomcat7:run that I ran into the following exception. This exception is the same one you’ll get at this point if you mvn install everything again and run as you just did (only this time you should have the default scope of compile on the raml-jaxrs-codegen-core dependency):
So what does this stack trace tell us? Apparently, javax.ws.rs.core.Application is being loaded from some jar but it doesn’t have the getProperties() method which returns a java.util.Map.
When you get this sort of behaviour; a program works fine when executed from your IDE (for instance), but gives an exception when executed in another environment (from Maven in this case), you have good reason to believe you’ve got a Jar hell issue to deal with. By “this sort of exception” I mean an exception which hints at the fact that you could have more than one jar in your classpath which has the same class name in the same package but with a different implementation. Class cast exceptions and no such method errors are common indicators of Jar hell. Again, by themselves they’re not, but when you’re getting this sort of thing when running in a particular environment but not in another, then that’s a pretty good sign you’ve got Jar hell issues.
Ok, so now we’re in trouble. Obviously, we can’t just rely on chance, hoping the right class is loaded in the environment we’re deploying to. Technically, if I had mvn package on the Maven module mentioned in passing above, I would get my war file and deploy it to my local installation of Tomcat, in which – as it so happens – the right class gets loaded and this exception doesn’t rear its ugly head. But that’s not solving the problem.
What we really need here is more information. It would be great if the NoSuchMethodError also gave us the location of the jar from which the javax.ws.rs.core.Application class was being loaded when things crashed. We don’t get that but we can use a tool called JHades to let us know if we have duplicate classes in the jars in our classpath. That information will practically solve this issue.
Go ahead and add jhades (not jhades-standalone-report) to contacts-app-impl’s pom – use latest version from here:
Now put the following in contacts-app-impl’s Main.java’s main method (at the beginning):
When you install and run as before, you should get a listing of overlapping jars in the console output along with the previous error message. The important part is the following:
file:/Users/justin/.m2/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar overlaps with
file:/Users/justin/.m2/repository/javax/ws/rs/javax.ws.rs-api/2.0/javax.ws.rs-api-2.0.jar - total overlapping classes: 55 - same classloader ! This is an ERROR!
We know from the error message that the problematic class is javax.ws.rs.core.Application, so it makes sense to focus on the clash involving jsr311-api-1.1.1.jar and javax.ws.rs.api-2.0.jar. Sure enough, if we open up the javax.ws.rs.core.Application class in both of these jars, we can see that the one in jsr311-api-1.1.1.jar does not have the getProperties() method which our code is trying to use. The one in javax.ws.rs.api-2.0.jar does. Note, if you don’t have the sources in your local repo, you should be able to get them by running mvn dependency:sources in the contacts-app parent module’s directory.
Finally, we’re almost at the root of the problem here. The final piece of the puzzle is figuring out what’s pulling in jsr311-api-1.1.1.jar and removing it. Since we actually started with the solution, you already know that contacts-app-api’s dependency on raml-jaxrs-codegen-core is pulling in jsr311-api-1.1.1.jar – but assuming you didn’t, how would you go about it?
What I did was simply dump out the output of mvn dependency:tree into a temporary file: mvn dependency:tree >> dependency-tree and search for jsr311 (or maybe grep for it with some context e.g. mvn dependency:tree | grep -C 20 jsr311 if you don’t want to stay creating a file). Either way, we get the info we need:
The code generator is pulling in the jar we want out, and come to think of it, it really makes no sense to include the raml-jaxrs-codegen-core as a dependency which is carried over when contacts-app-api is depended upon in another project. The code generator is only used to generate code – i.e. to build our contacts-app-api. It is not something we need at runtime. Therefore, using the scope of provided on the code generator will have the effect we’re after, to include the dependency while building contacts-app-api but not when depending on it from another module.
What I’d like to quickly demo now is how to use JHades in other scenarios. Let’s replicate the issue we are having here in the context of a war Maven module, where we don’t have the luxury of simply dropping in some code to print out the overlapping jars.
So lets go ahead and add another Maven module – `contacts-app-server`. Now our project structure looks something like this:
We’ll mkdir -p src/main/webapp/WEB-INF in contacts-app-server and add web.xml in WEB-INF:
Nothing special here. We’ve practically just moved over from running the app from Main.java to being able to package it up as a war and run it on a servlet container like Tomcat. (Note you can get the pom.xml which is not shown here for brevity from the demo repo under the branch with the same name as this blog post – just remember that the code generator does have the scope of provided there).
If you try running contacts-app-server using mvn clean tomcat7:run you will get this error:
java.lang.ClassCastException: org.glassfish.jersey.servlet.ServletContainer cannot be cast to javax.servlet.Servlet
This is actually an error which has nothing to do with what we’re concerned with. I am not entirely sure why but the Jetty dependencies we have in contacts-app-impl are causing this. I am able to open up the ServletContainer in my IDE and see that the HttpServlet it’s extending is being picked up from a jar with Maven coordinates: org.eclipse.jetty.orbit:javax.servlet:3.0.0.v201112011016 – (looks like one crazy version number).
Anyway, we will not be using Main.java to host our web app so go ahead and delete it along with the Jetty dependencies in contacts-app-impl’s pom.xml. Then you should be able to run tomcat from maven again and get the NoSuchMethodError from before.
In order to get the info we need here, we have two options (at least I know of 2 options). We can either drop in the dependency on JHades as we did before and add the following to our web.xml (at the start):
This should again give us the info we need (append the output to a temporary file).
Alternatively, you can get your hands on the jhades-standalone-report jar (either download it manually or depend on it and let Maven install it in your local repo). This allows us to run the following command on our project’s output (the war file):
java -jar /Users/justin/.m2/repository/org/jhades/jhades-standalone-report/1.0.4/jhades-standalone-report-1.0.4.jar ./target/contacts-app-server-1.0.0-SNAPSHOT.war
Note: using mvn tomcat7:run seems to remove the war file from the target directory so you’ll have to mvn package again.
Either way, you’ll be able to get to the same conclusion as before with the output from JHades.
I will eventually come round to writing the blog post I initially had in mind (this one just kind of developed while trying to go for something else). Tune in again next time for yet another module we’ll be adding to this project 🙂
Next time we will be going through setting up a front-end module using Yeoman (using the backbone generator) – build our project with Grunt from Maven and depend on our shiny new front-end module from our contacts-app-server.
Until next time 🙂