6

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.

20

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
  • for 45, the exact compiler version depends on the minor version. 45.3 would be 1.0.2 I beleive. – Antimony Apr 15 '13 at 17:13
  • 1
    This answer should have been the accepted one: javap works on Windows AND Linux. – avgvstvs Apr 18 '14 at 13:56
  • 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. – David Phillips Dec 29 '18 at 3:50
16

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)
  • What if you're on Windows? – avgvstvs Apr 18 '14 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 Apr 27 '14 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 Apr 27 '14 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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.