8

I have a java class file. How do I find out the version of the compiler used to compile this file? I'm on Ubuntu Server 12.04.

3 Answers 3

22

The JDK includes a javap command. It gives a lot information, but you can use it like this:

javap -verbose yourClass | grep version

Example output:

  minor version: 0
  major version: 51

The major version tells you which version the compiler had:

J2SE 8 = 52,
J2SE 7 = 51,
J2SE 6.0 = 50,
J2SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45
3
  • for 45, the exact compiler version depends on the minor version. 45.3 would be 1.0.2 I beleive.
    – Antimony
    Commented Apr 15, 2013 at 17:13
  • 1
    This actually tells you the version of the class file format, not the version of the compiler or other tool used to generate it. javac supports the -target or --release flag to generate class files targeted at earlier versions of the JDK. Commented Dec 29, 2018 at 3:50
  • Is there a way to get the version but in the code ?
    – Ayfri
    Commented May 11, 2020 at 16:35
15

Again the file(1) utility and libmagic(3), on which it is based, can be your friend:

$ file Gwan.class
Gwan.class: compiled Java class data, version 50.0 (Java 1.6)
3
  • What if you're on Windows?
    – avgvstvs
    Commented Apr 18, 2014 at 13:55
  • 1
    @avgvstvs: then you can still resort to something like Cygwin. Honestly, if you limit your own toolset by restricting your RCE efforts to a single host OS, that's your own fault. Simply put: know what tools to use when and don't be picky based on arbitrary and artificial limitations.
    – 0xC0000022L
    Commented Apr 27, 2014 at 18:24
  • 3
    Cygwin introduces an unnecessary dependency. If you're reversing .class files, on *every JVM you're going to have access to javap. I don't disagree with the rest of what you're saying, but I'm lazy and if I don't have to start a vm or d/l cygwin I'm happier.
    – avgvstvs
    Commented Apr 27, 2014 at 19:52
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.