Skip to content

Instantly share code, notes, and snippets.

@ecelis
Last active August 29, 2015 14:28
Show Gist options
  • Save ecelis/93080a9ba282d2bf3784 to your computer and use it in GitHub Desktop.
Save ecelis/93080a9ba282d2bf3784 to your computer and use it in GitHub Desktop.
Java Version Checker
import java.io.*;
/* Code taken from http://www.rgagnon.com/javadetails/java-0544.html
Usage
> java ClassVersionChecker ClassVersionChecker.class
ClassVersionChecker.class: 49 . 0
*/
public class ClassVersionChecker {
public static void main(String[] args) throws IOException {
for (int i = 0; i < args.length; i++)
checkClassVersion(args[i]);
}
private static void checkClassVersion(String filename)
throws IOException
{
DataInputStream in = new DataInputStream
(new FileInputStream(filename));
int magic = in.readInt();
if(magic != 0xcafebabe) {
System.out.println(filename + " is not a valid class!");;
}
int minor = in.readUnsignedShort();
int major = in.readUnsignedShort();
System.out.println(filename + ": " + major + " . " + minor);
in.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment