UnsupportedClassVersionError workaround hack

I tried to run an "iText toolbox" tool but got:

Exception in thread "main" java.lang.UnsupportedClassVersionError:
com/lowagie/toolbox/Toolbox (Unsupported major.minor version 49.0)

In the target environment, upgrade of Java is out of question. Therefore, I had a choice:

* recompile the library, or
* force Java to accept the older version class.

The second way is more fun. And unexpectedly not hard. The search on "java class format" revealed the specification. A fix is very simple: just two shorts, one is minor version and one is major, and no checksum. A Python program:

MINOR = 0x00
MAJOR = 0x2e00 # Not 0x2e: big-endian Java vs Intel CPU

import sys, struct
s_version = struct.pack('2H', MINOR, MAJOR)

def fix_file(fname):
  h = open(fname, 'r+')

for fname in sys.argv[1:]:

The not-so-robust program fixes a set of files, which are given as command line arguments.

To fix classes in jar:

$ rm -rf com/ META-INF/
$ jar xf ../iText-toolbox-2.1.7.jar 
$ find . -name '*.class' -print0 | xargs -0 python set-class-version.py
$ jar cf iText-toolbox-2.1.7-olpa.jar com/ META-INF/

It worked!

Unfortunately, I got a new problem: the toolbox uses java.awt....Desktop, which appeared after Java 1.3. Therefore, there was a reason why a more recent version of Java required. And therefore, I had to modify the source code for my needs. But anyway, I liked the experiment.

Categories: java