Wednesday, January 19, 2011

Find the java version used to compile a java class


Every '.class' file starts off with the following:

* Magic Number [4 bytes]
* Version Information [4 bytes]

A hexdump of a '.class' file compiled with each of the following options
reveals:

javac -target 1.1 ==> CA FE BA BE 00 03 00 2D
javac -target 1.2 ==> CA FE BA BE 00 00 00 2E
javac -target 1.3 ==> CA FE BA BE 00 00 00 2F
javac -target 1.4 ==> CA FE BA BE 00 00 00 30

Perhaps you could use this information to write your own '.class' file
version checking utility, using Java, or perhaps a scripting or shell
language ;) !

I hope this helps.

Anthony Borla

P.S.

If requiring more information just do a Google search for ".class file
format" or "Java Bytecode Tutorial"


From: http://bytes.com/topic/java/answers/16603-how-determine-java-bytecode-version



Alternatively 'javap' utility that comes with standard JDK can be used


javap -verbose MyClass

Compiled from “MyClass.java”
public class MyClass extends java.lang.Object
SourceFile: “MyClass.java”
minor version: 3
major version: 45

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




Couple of more options can be found at the post:

http://stackoverflow.com/questions/698129/how-can-i-find-the-target-java-version-for-a-compiled-class

No comments: