J2SDK 1.5.0 Beta 1, Class File Versions, Compatibility, ...
When I tried the J2SE 1.5.0 Beta 1 nine days ago, I was met with a slew of exceptions, including a java.lang.ClassFormatError:
Thrown when the Java Virtual Machine attempts to read a class file and determines that the file is malformed or otherwise cannot be interpreted as a class file.
To find out why, I compiled "interface X{}" with both 1.4.2_03 and 1.5.0 and compared the results:
[weiqi@gao] $ ls -l X.class.* -rw-rw-r-- 1 weiqi weiqi 83 Feb 14 06:28 X.class.1.4.2 -rw-rw-r-- 1 weiqi weiqi 83 Feb 14 06:32 X.class.1.5.0 [weiqi@gao]$ od -t x1 X.class.1.4.2 0000000 ca fe ba be 00 00 00 2e 00 07 07 00 05 07 00 06 0000020 01 00 0a 53 6f 75 72 63 65 46 69 6c 65 01 00 06 0000040 58 2e 6a 61 76 61 01 00 01 58 01 00 10 6a 61 76 0000060 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74 06 00 00 0000100 01 00 02 00 00 00 00 00 00 00 01 00 03 00 00 00 0000120 02 00 04 0000123 [weiqi@gao]$ od -t x1 X.class.1.5.0 0000000 ca fe ba be 00 00 00 30 00 07 07 00 05 07 00 06 0000020 01 00 0a 53 6f 75 72 63 65 46 69 6c 65 01 00 06 0000040 58 2e 6a 61 76 61 01 00 01 58 01 00 10 6a 61 76 0000060 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74 06 00 00 0000100 01 00 02 00 00 00 00 00 00 00 01 00 03 00 00 00 0000120 02 00 04 0000123 [weiqi@gao]$ jcf-dump X.class.1.4.2 Reading .class from X.class.1.4.2. Magic number: 0xcafebabe, minor_version: 0, major_version: 46. ... [weiqi@gao]$ jcf-dump X.class.1.5.0 Reading .class from X.class.1.5.0. Magic number: 0xcafebabe, minor_version: 0, major_version: 48. ...
So the major version number of class files generated by default from javac has been bumped up, from 46 to 48.
On a practical level, this means that JODE 1.1.1 won't work with 1.5.0 anymore, even for java.lang.Object. :(
The class file version number is controlled by the -target switch on the command line:
| -target | major | minor | note |
|---|---|---|---|
| 1.1 | 45 | 3 | 1.4.2_03 only |
| 1.2 | 46 | 0 | -source 1.2 switch needed for 1.5.0 |
| 1.3 | 47 | 0 | -source 1.3 switch needed for 1.5.0 |
| 1.4 | 48 | 0 | |
| 1.5 | 49 | 0 | 1.5.0 only |
All of this indicates that the upgrade from 1.4 to 1.5 will be harder than the upgrade from 1.2 to 1.3 or from 1.3 to 1.4. It will more likely be as hard as the upgrade from 1.1 to 1.2. Remember that?