How to determine whether jar is compiled with Java 1.5
Our application uses many third party jars. We need to run under Java 1.4 JVMs so we need to determine whether a jar has been compiled under Java 1.5 and would therefore cause a version mismatch error at runtime.
Is there a way to determine whether a jar will run without problem under Java 1.4 JVM?
thanks,
Peter
A jar file is not compiled under Java 1.5, only the class files within it.
So you would have to check every class file within a Jar file, to see whether it has been compiled under JDK 1.5
The major and minor numbers of the class file header will tell you this.
You may have to create a JDK 1.5 class, and run it through this method to find the exact major and minor numbers corresponding to Java 1.5.
It is not as simple as "1.5".... the major and minor numbers correspond to class file format versions... along the lines of "49.0"
If you want to be clever... you could write a custom Ant task to run through every jar in your build system, iterate through the Jar file entries for *.class, and test whether they were compiled under 1.5 or not.
Note : this test will only tell whether the class file was compiled under 1.5.
Not whether is could potentially run under 1.4.2 if it were compiled with 1.4.2 instead.
regards,
Owen
public void simpleExample ()
{
FileInputStream fis = new FileInputStream ("mytest.class");
parseJavaClassFile ( fis );
}
protected void parseJavaClassFile ( InputStream classByteStream ) throws Exception
{
DataInputStream dataInputStream = new DataInputStream ( classByteStream );
magicNumber = dataInputStream.readInt();
if ( magicNumber == 0xCAFEBABE )
{
int minorVer = dataInputStream.readUnsignedShort();
int majorVer = dataInputStream.readUnsignedShort();
// do something here with major & minor numbers
}
}
I would also have to determine how the major/minor numbers are set when a class is cross-compiled in 1.5 for 1.4 (the "target=1.4" compiler switch). I'll test it, but I bet the version number will reflect the target version, rather than the compiler version.
Thank you for your response, Owen.
Regards,
Peter
> I would also have to determine how the major/minor numbers are
> set when a class is cross-compiled in 1.5 for 1.4 (the "target=1.4"
> compiler switch).
They'll reflect the target, but that's no guarantee that the class file doesn't use library methods which were introduced in 1.5.