Thursday, June 27, 2013

Java version of a .class

Sometime ago I had an issue with a code that was sent to us already compiled (no sources attached). During deployment we were facing an exception like this one: javax.servlet.ServletException: Bad version number in .class file

What I found reading on the web about this problem is that the problem was probably caused by compiling the Java project in a Java version higher than the one running in the JVM on the server. In my particular case I found that the code was compiled in Java 6, while the JVM was running Java 5.

To confirm this I learned a trick on how to find the Java version of a .class. For this there is a command: javap -verbose ClassName
(The .exe can be found in the JDK bin directory)

It is recommended to save the result of the command on a file since it throws a lot of information. What you need to look for is the combination of minor and major version that is found in the beginning. These two values are the key to determine the Java version used to compile the class.

major  minor Java platform version 
45       3           1.0
45       3           1.1
46       0           1.2
47       0           1.3
48       0           1.4
49       0           1.5
50       0           1.6