14

Duplicate:

Tool to read and display Java .class versions

If I have a compiled Java class, is there a way to tell from just the class file what its target version compatibility is? Specifically, I have a number of class files, compiled to Java 6, which are running under Java 5 and giving the the "Unrecognized version" error. I want to be able to look at a class file and find what its target version compatibility is without running the JVM. Any ideas?

Community
  • 1
  • 1
Mike Pone
  • 18,705
  • 13
  • 53
  • 68
  • ... and of course, the file utility is also included on MacOS, if you're willing to use the Terminal. – kmorris Jul 25 '13 at 02:46

5 Answers5

45

You can use the javap utility that comes with the standard JDK.

javap -verbose MyClass

Compiled from “MyClass.java”
public class MyClass extends java.lang.Object
SourceFile: “MyClass.java”
minor version: 3
major version: 45
bruno conde
  • 47,767
  • 15
  • 98
  • 117
  • while it may be considered "better" it is not exactly what I was looking for. – Mike Pone Mar 30 '09 at 17:47
  • 4
    Same result can be achieved using file program (on most GNU/unix system) : file Foo.class Foo.class: compiled Java class data, version 45.3 BTW: how to compile to produce this output (45.3) , is .45 jdk1.x or j2me ? – RzR Apr 01 '11 at 08:44
  • 6
    There is a table of major version numbers (like major version 51 = Java 1.7 and so on), on the [Wikipedia article for Java class file](http://en.wikipedia.org/wiki/Java_class_file#General_layout). – rescdsk Jan 05 '12 at 17:49
  • 1
    The exact version can be dumped using "javap -version target.class" – user2149140 Mar 28 '18 at 21:45
  • 1
    @user2149140 `javap -version` reports the version of `javap`, not your `target.class` – Cardin Aug 14 '20 at 05:58
25

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

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Kalecser
  • 1,143
  • 12
  • 15
5

Linux/Unix users have a nice tool out of the standard toolbox: file utility. Modern versions can detect the Java class fersion version and even output Java version for known class file types.

Example output:

com/sample/Tracker.class: compiled Java class data, version 45.3
com/sample/TestListener.class: compiled Java class data, version 49.0 (Java 1.5)

And it fits very nicely into the standard Unix scripting toolchain.

user2240219
  • 166
  • 1
  • 2
  • 2
3

Taken from: http://twit88.com/blog/2008/09/22/java-check-class-version/

try {
    String filename = "C:\\MyClass.class";
    DataInputStream in = new DataInputStream(new FileInputStream(filename));
    int magic = in.readInt();
    if (magic != 0xcafebabe) {
        log.info(filename + " is not a valid class!");
    }
    int minor = in.readUnsignedShort();
    int major = in.readUnsignedShort();
    log.info(filename + ": " + major + " . " + minor);
    in.close();
} catch (IOException e) {
    log.info("Exception: " + e.getMessage(), e);
}
Parker
  • 7,244
  • 12
  • 70
  • 92
Kevin Crowell
  • 10,082
  • 4
  • 35
  • 51
0

You can look at the byte offset 6 and 7 in the file (in a hex dump probably), which tells you which version is used. I think the Bytecode Visualizer (eclipse plugin) can see which version a class file is made for.

Further reading

Björn
  • 29,019
  • 9
  • 65
  • 81