Fixing Classpath Issues with JasperReports, J2EE, and Maven

If you are having issues with bundling JasperReports, a J2EE server, and Maven, you are not alone. There are many bumps in the road for getting JasperReports integrated with Tomcat/J2EE Container and building with Maven. Hopefully, this blog entry will make things slightly easier.

Firstly, there is the confusion of where the JasperReports dependency lies in the maven2 repository. There is a very suggestive entry for: {groupid: jasperreports, artifactid: jasperreports} however, that entry only hosts versions 0.5.0 to 3.5.3. The entry: {groupid: net.sf.jasperreports, artifactid: jasperreports} contains the versions 3.6 to the latest version. In addition, the last entry contains an artifactid of jasperreports-fonts, handy if special reporting fonts are requested from your reports.

Secondly, if you are getting ClassNotFoundException or NoClassDefFoundError for the class: net/sf/jasperreports/engine/JRException your issue with still with the POM configuration for JasperReports. The ClassNotFoundException is a runtime error, so despite a successful build, you will still see this. Despite the  jasperreports-x.x.x.jar file being in your web-inf folder, you may still see this error.

You must modify the scope of the component in the pom.xml. Change the scope from compile [which is needed to build], to “provided.” ( Provided not only uses the dependency to build, but update’s the container’s classpath to use this as a runtime dependency.

A sample POM excerpt:


One of the other solutions to this classpath issue, which is frequently suggest by others, is to add the JasperReport jars to your TomCat libs or libs-ext folder. This solution works, however it is a very bad solution. By following the advice, you now create a dependency on the server rather than the project, and create potential upgrading migration issues on the server, your project, and for potential customer support calls. [The server could be upgraded/downgraded and your project would no longer be able to guarantee the correct version].