As mentioned in my earlier post, “Getting JasperReports 5.x to Operate Smoothly with Java 8”
Upgrading from Java 7 to 8 can involve a lot of moving parts, and there are a number of versions of common libraries that may not work well with Java 8, but most of them can be made to work with Java 8 with a little effort.
Here is another library – Drools 5.x – with upgrade issues. The standard suggestion is to upgrade to Drools 6, but there are enough changes involved in that effort that you may not wish to do that upgrade at the same time. This post will allow you to effectively decouple your Java upgrade from your Drools upgrade. Before going into more details of the workaround or proposed solution, let’s see what the out-of-the-box run-time behavior of Drools 5.x is with Java 8 when compiling rule templates / decision tables where data providers are spreadsheets:
If you observe the exception stack trace, the exception message “wrong class format” and a little more detail on the root cause – “org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException” – clearly indicates that the exception is thrown by a class file reader when encountering an error in decoding information contained in a .class file. Now, let’s check who is responsible for this compilation process. If we look at the package for the exception class and also look at the dependency graph for Drools libraries:
There are two libraries with issues in this tree: JDT Core Batch Compiler version 3.5.1 and MVEL version 2.1.3.Final. The issue with JDT Core is more straightforward - Java 8 requires at least version 4.4.
Here is gradle script snippet with the fix we tried which works to some extent.
Technically, we are asking gradle to resolve “org.eclipse.jdt.core.compiler:ecj” to the most current version in the 4.4.x series. You can read the Gradle documentation for more details on dependency resolution.
So, this fixes the first issues with JDT Core, but it just surfaces another.
At this point, we considered replacing JDT completely with Janino, but we looked at the bottom of the stack trace, and decided to see what we could fix at the MVEL level. And digging through MVEL code identified the root cause in the lines highlighted below.
Because of this code when running with Java 8, the OPCODES_VERSION been considered as OpCodes.V1_2, which is causing the above exception. Actually, this is handled correctly in very recent versions of MVEL code, but we cannot go ahead with MVEL latest version - MVEL2.2.8 or later, as it may not work well with Drools 5.x. So, we updated the code as below. We can do this because MVEL source code is released under the Apache license, which allows us to modify the code:
If you observe, we are checking to see whether the Java version is 1.6 or 1.7 or 1.8; we are considering OPCODES_VERSION as 1.6, which is compatible and works in this context and it’s not necessary to pull all complete MVEL Java 8 specific implementations. There are already a few requests to MVEL to release our proposed fix as part of 2.1.x series but there are no actions yet taken. So, we decided to build the patched version of MVEL 2.1.3.Final with the below fix and published as “MVEL-2.1.3.Final-Patch” in our enterprise repository and started consuming it along with the above-mentioned fix across CCC projects.
Here is the final Gradle script snippet with above-mentioned approach.
With this approach, we de-coupled the Java 8 upgrade from Drools 6 upgrade. I hope this solution may help people who may be stuck with these issues and have run into challenges upgrading to Drools 6 along with Java 8.