Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool to read and display Java .class versions

Tags:

jvm-bytecode

People also ask

How do I view the contents of a .class file?

A simple way to see what String literals are used in a ". class" file is to use the javap utility in your JDK installation to dump the file using the "-v" option. Then grep for text that looks like <String "..."> where ... is the String you are looking for.

How can I tell what version of Java a compiled class is?

In Java, we can use javap -verbose className to print out the class information. D:\projects>javap -verbose Test Classfile /D:/projects/Test. class Last modified 16 Apr 2019; size 413 bytes MD5 checksum 8679313dc0728e291898ad34656241cb Compiled from "Test.

Can Java read .class files?

javap is the Java Disassembler tool which can be used to open a . class file in a readable format. javap is located in the /bin folder of the JDK installation directory. The Java Decomplier (javap) displays information about the package, protected and public fields, and methods of the classes passed to it.

How do I read a .class file bytecode?

Using javap. The Java command-line comes with the javap tool that displays information about the fields, constructors, and methods of a class file. Based on the options used, it can disassemble a class and show the instructions that comprise the Java bytecode.


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

It is easy enough to read the class file signature and get these values without a 3rd party API. All you need to do is read the first 8 bytes.

ClassFile {
    u4 magic;
    u2 minor_version;
    u2 major_version;

For class file version 51.0 (Java 7), the opening bytes are:

CA FE BA BE 00 00 00 33

...where 0xCAFEBABE are the magic bytes, 0x0000 is the minor version and 0x0033 is the major version.

import java.io.*;

public class Demo {
  public static void main(String[] args) throws IOException {
    ClassLoader loader = Demo.class.getClassLoader();
    try (InputStream in = loader.getResourceAsStream("Demo.class");
        DataInputStream data = new DataInputStream(in)) {
      if (0xCAFEBABE != data.readInt()) {
        throw new IOException("invalid header");
      }
      int minor = data.readUnsignedShort();
      int major = data.readUnsignedShort();
      System.out.println(major + "." + minor);
    }
  }
}

Walking directories (File) and archives (JarFile) looking for class files is trivial.

Oracle's Joe Darcy's blog lists the class version to JDK version mappings up to Java 7:

Target   Major.minor Hex
1.1      45.3        0x2D
1.2      46.0        0x2E
1.3      47.0        0x2F
1.4      48.0        0x30
5 (1.5)  49.0        0x31
6 (1.6)  50.0        0x32
7 (1.7)  51.0        0x33
8 (1.8)  52.0        0x34
9        53.0        0x35

On Unix-like

file /path/to/Thing.class

Will give the file type and version as well. Here is what the output looks like:

compiled Java class data, version 49.0


If you are on a unix system you could just do a

find /target-folder -name \*.class | xargs file | grep "version 50\.0"

(my version of file says "compiled Java class data, version 50.0" for java6 classes).


Yet another java version check

od -t d -j 7 -N 1 ApplicationContextProvider.class | head -1 | awk '{print "Java", $2 - 44}'