Thursday, November 30, 2017

Java version in the .class file

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/eclipse/jetty/util/security/Password : Unsupported major.minor version 52.0

java.lang.UnsupportedClassVersionError happens because of a higher JDK during compile time and lower JDK during runtime.


1. fos@ubuntu:~/dev/java$ javap -v HelloWorld |grep version
  minor version: 0
  major version: 52
2. fos@ubuntu:~/dev/java$ file HelloWorld.class
HelloWorld.class: compiled Java class data, version 52.0 (Java 1.8)

3. Find the value in 6th and 7th bytes:
Within Emacs:
87654321  0011 2233 4455 6677 8899 aabb ccdd eeff  0123456789abcdef
00000000: cafe babe 0000 0034 0041 0a00 1000 1d07  .......4.A......

Or Use hexdump:
fos@ubuntu:~/dev/java$ hexdump HelloWorld.class |more
0000000 feca beba 0000 3400 4100 000a 0010 071d

0x34 = 52  <--- 8="" is="" java="" p="" this="">


Here's the list of versions:

Java SE 9 = 53,
Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

More:
The class file format: https://docs.oracle.com/javase/specs/jvms/se6/html/ClassFile.doc.html

No comments: