How to Check the Jdk Version Used to Compile a .Class File

how to check the jdk version used to compile a .class file

You're looking for this on the command line (for a class called MyClass):

On Unix/Linux:

javap -verbose MyClass | grep "major"

On Windows:

javap -verbose MyClass | findstr "major"

You want the major version from the results. Here are some example values:

  • Java 1.2 uses major version 46
  • Java 1.3 uses major version 47
  • Java 1.4 uses major version 48
  • Java 5 uses major version 49
  • Java 6 uses major version 50
  • Java 7 uses major version 51
  • Java 8 uses major version 52
  • Java 9 uses major version 53
  • Java 10 uses major version 54
  • Java 11 uses major version 55

Get java version that was used to compile class

Yes. You can find the version of JDK on which the class was compiled. Refer here.

Tool to read and display Java .class versions

Use the javap tool that comes with the JDK. The -verbose option will print the version number of the class file.

> javap -verbose MyClass
Compiled from "MyClass.java"
public class MyClass
SourceFile: "MyClass.java"
minor version: 0
major version: 46
...

To only show the version:

WINDOWS> javap -verbose MyClass | find "version"
LINUX > javap -verbose MyClass | grep version

Does the JDK version being used to compile the code matter?

No, you can't necessarily run code compiled with a new JDK on an old JRE. Compiled classes contain a version number for the class file format; if this is newer than the runtime expects, it will refuse to load the class.

Most Java compilers support an option to target an older JRE, generating an older class file format than the compiler was built for. However, you can still run into trouble if you don't also compile against an older version of the Java runtime library. Your code might use new API that isn't in the older version of Java. The compiler, with its current version of the API, won't catch this even when you specify an older target.

For the standard javac compiler in OpenJDK, these options are -target and -bootclasspath. You might also want to set the -source option to catch usage of newer language features that require support the older class files don't provide.

Java API to find out the JDK version a class file is compiled for?


import java.io.*;

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();
}
}

The possible values are :

major  minor Java platform version 
45 3 1.0
45 3 1.1
46 0 1.2
47 0 1.3
48 0 1.4
49 0 5
50 0 6
51 0 7
52 0 8
53 0 9
54 0 10
55 0 11
56 0 12
57 0 13
58 0 14

find the compiled class version number

You can use javap (with -v for verbose mode), and specify any class from the jar file. For example, looking at a Joda Time jar file:

javap -cp joda-time-2.7.jar -v org.joda.time.LocalDate

Here the -cp argument specifies the jar file to be in the classpath, the -v specifies that we want more verbose information, and then there's the name of one class in the jar file.

The output starts with:

Classfile jar:file:/c:/Users/Jon/Test/joda-time-2.7.jar!/org/joda/time/LocalDate.class
Last modified 12-Jan-2015; size 16535 bytes
MD5 checksum d19ebb51bc5eabecbf225945eccd23ef
Compiled from "LocalDate.java"
public final class org.joda.time.LocalDate extends org.joda.time.base.BaseLocal implements org.joda.time.ReadablePartial,java.io.Serializable
minor version: 0
major version: 49

The "minor version" and "major version" bits are the ones you're interested in.

It's possible that a single jar file contains classes compiled with different versions, of course.

How can I find the target Java version for a compiled class?

I've found this on the net and it works.

Every '.class' file starts off with
the following:

  • Magic Number [4 bytes]
  • Version Information [4 bytes]

A hexdump of a '.class' file compiled
with each of the following options
reveals:

javac -target 1.1 ==> CA FE BA BE 00 03 00 2D

javac -target 1.2 ==> CA FE BA BE 00 00 00 2E

javac -target 1.3 ==> CA FE BA BE 00 00 00 2F

javac -target 1.4 ==> CA FE BA BE 00 00 00 30

Perhaps you could use this information
to write your own '.class' file
version checking utility, using Java,
or perhaps a scripting or shell
language ;) !

I hope this helps.

Anthony Borla

From: http://bytes.com/groups/java/16603-how-determine-java-bytecode-version

What version of javac built my jar?

You can't tell from the JAR file itself, necessarily.

Download a hex editor and open one of the class files inside the JAR and look at byte offsets 4 through 7. The version information is built in.

http://en.wikipedia.org/wiki/Java_class_file

Note: As mentioned in the comment below,

those bytes tell you what version the class has been compiled FOR, not
what version compiled it.



Related Topics



Leave a reply



Submit