Thursday, March 11, 2010

What Java Compiler Compiled this class file.

I wrote a quick perl script to determine what version of javac
was used to compile a .class file.
Its pretty simple perl code, so here it is.
Save this to a file and make it executable


#!/usr/bin/perl -w
# Prints the version of the java compiler used to compile the class file.
# http://en.wikipedia.org/wiki/Class_(file_format)#File_layout_and_structure
#
use strict;

my %version = (
45 => "JDK 1.1",
46 => "JDK 1.2",
47 => "JDK 1.3",
48 => "JDK 1.4",
49 => "J2SE 5.0",
50 => "J2SE 6.0"
);


foreach my $ARG (@ARGV){
my $input = "";
open(FILE,"< $ARG") || die " Couldn't open $ARG";
read(FILE, $input,8) || die "Couldn't read 8 bytes.";
close(FILE);

my ($cafebabe,$minor,$major) = unpack("Nnn",$input);

if($cafebabe == 0xCAFEBABE){
my $compiler = $version{$major};
if(defined $compiler){
print "$ARG: Java Compiler was $compiler minor=$minor\n";
} else {
print "$ARG: Java Compiler was UNKNOWN major=$major minor=$minor\n";
}
} else {
printf "$ARG: Not a java file! Magic=%x\n",$cafebabe;
}

}

No comments:

Post a Comment