Saturday, March 31, 2007

Class file demand for which Java version

Someday back I got a call from my roommate's friend asking about that he has moved some class files from one system to other and now those class files are not running. I guess he got those class files from somewhere and he has no idea about which version of JDK it will run.

It's not such a tough job to find out that which version of JDK your class file is demanding. What we need to know is the format of class file and rest is like in your hands. Just look at the code for getting more clear picture.

import java.io.*;
class CheckClassFileFormat {
public static void main(String[] args) throws Exception
{
int magicNumber;
int minorVersion;
int majorVersion;
// and many more
DataInputStream data = new DataInputStream(new FileInputStream(args[0]));
magicNumber = data.readInt();
if(magicNumber !=0xcafebabe) // why 0xcafebabe.. read it, its so funny
{
System.out.println("Not a correct class format");
System.exit(1);
}
minorVersion = data.readShort();
majorVersion = data.readShort();
String version ="no version";

/*The Java virtual machine implementation of Sun's JDK release 1.0.2 supports class file format versions 45.0 through 45.3 inclusive. Sun's JDK releases 1.1.X can support class file formats of versions in the range 45.0 through 45.65535 inclusive. Implementations of version 1.2 of the Java 2 platform can support class file formats of versions in the range 45.0 through 46.0 inclusive.*/

if (majorVersion < 48) {
version = "jdk1.3.1";
} else if (majorVersion == 48) {
version = "jdk1.4.2";
} else if (majorVersion == 49) {
version = "jdk1.5";
} else {
version = "jdk 6";
}
// can follow up the same line for new releases.

System.out.println(args[0] + ": correct jdk version is " + version);
}
}
Just pass the classfile (like Test.class) in argument and we get to know on which JDK it need to run :)

4 comments:

@nkur said...

Awesome!

Abhishek said...

Ha Ha Ha
First Java tip you wrote about that I knew already :)
Ye Ye Ye!!!

Vaibhav said...

Thanks Ankur !

Ah cool Abhishek , ya this is a common problem most of us face in general, so I just went ahead and wrote a small code for it :)

Anonymous said...

miley cyrus nude [url=http://www.ipetitions.com/petition/mileycyrus]miley cyrus nude[/url] paris hilton nude [url=http://www.ipetitions.com/petition/parishilt]paris hilton nude[/url] kim kardashian nude [url=http://www.ipetitions.com/petition/kimkardashian45]kim kardashian nude[/url] kim kardashian nude [url=http://www.ipetitions.com/petition/celebst]kim kardashian nude[/url]