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

[339 byte] By [peter.wilkinsa] at [2007-10-3 2:43:04]
# 1
Someone feel free to correct me.I don't know how to get the version from a class file but you could check the manifest file. Quite often the JDK version used will be present.Ted.
ted_trippina at 2007-7-14 20:31:24 > top of Java-index,Java Essentials,Java Programming...
# 2

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

}

}

omcgoverna at 2007-7-14 20:31:24 > top of Java-index,Java Essentials,Java Programming...
# 3

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

peter.wilkinsa at 2007-7-14 20:31:24 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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.

YAT_Archivista at 2007-7-14 20:31:24 > top of Java-index,Java Essentials,Java Programming...
# 5
Isnt there a saying that says: if you want to find the easiest way todo something ask a lazy person to do it...why dont you just run the program under 1.4 and see if it crashes?
TuringPesta at 2007-7-14 20:31:24 > top of Java-index,Java Essentials,Java Programming...
# 6
The compiler will return an error if target=1.4 is set and the code contains 1.5 methods.
peter.wilkinsa at 2007-7-14 20:31:24 > top of Java-index,Java Essentials,Java Programming...
# 7
Running the app would only verify that there was no version mismatch if we could verify that we had loaded all the dependent jars. That would become a code coverage test. Not a bad idea, but perhaps harder than some of the suggestions offered earlier in this thread.
peter.wilkinsa at 2007-7-14 20:31:24 > top of Java-index,Java Essentials,Java Programming...
# 8
> The compiler will return an error if target=1.4 is set and the code contains 1.5 methods.Wrong. The compiler will return an error if -target 1.4 is used and the code contains 1.5 language features, but it has no idea when classes and methods were added to the API.
YAT_Archivista at 2007-7-14 20:31:24 > top of Java-index,Java Essentials,Java Programming...