tags:

views:

34

answers:

1

Why java 1.4 compiler does not complain about a third party library compiled with 1.5 compiler ? The problem is encountered at runtime UnsupportedClassVersionError .

A: 

As of Java 1.5, the class file format was changed slightly (to retain annotations, for instance). See the -target option in the compiler docs. Java 1.5 and later JVMs support both class file formats; Java 1.4 VMs (naturally) only support the earlier format.

As for why the compiler doesn't complain: The compiler need only concern itself with whether it has enough information to correctly compile the files that it's been asked to compile; not whether it can run them correctly. The JVM needs to worry about running the classes correctly. Apparently, if you're seeing the 1.4 compiler happily referencing 1.5 classes from within the code it's compiling, the format change didn't affect the information the compiler needed to compile 1.4-style code. I can't immediately find a reference guaranteeing that behavior, but if it's what you're seeing...

T.J. Crowder
I don't see where you answer the question of *why*. Shouldn't Java 1.4 notice that the file used an unsupported format?
Rob Kennedy
@Rob: You're quite right, I actually misread the question entirely. I'll go fix that, then. :-)
T.J. Crowder
@TJ your comment does explain possible rational. Thanks. I wish the official reference was handy.
tech20nn

related questions