Wednesday, 18 May 2022

How to find the Java major and minor versions from a .class file

Every Java release internally maintains a major and minor version numbers.

 

Below table summarizes different major.minor versions of Jdk.

 

JDK version

Major version number

JDK 1.1

45

JDK 1.2

46

JDK 1.3

47

JDK 1.4

48

Java SE 5.0

49

Java SE 6.0

50

Java SE 7

51

Java SE 8

52

Java SE 9

53

Java SE 10

54

Java SE 11

55

Java SE 12

56

Java SE 13

57

Java SE 14

58

Java SE 15

59

Java SE 16

60

Java SE 17

61

 


When you compile a Java class file, major and minor version numbers will be added to the Java class file. Using javap command, we can read this from .class file.

 

HelloWorld.java

public class HelloWorld {
	
	public static void main(String[] args) {
		System.out.println("Hello World");
	}

}

Compile HelloWorld.java file.

$javac HelloWorld.java 
$
$ls
HelloWorld.class	HelloWorld.java

Execute the command ‘javap -verbose HelloWorld.class’ to see the content of .class file.

$javap -verbose HelloWorld.class
Classfile /Users/krishna/Documents/technical-documents/examples/HelloWorld.class
  Last modified 17 May, 2022; size 425 bytes
  MD5 checksum 1e678fa12f97b65fdde287539d5ce419
  Compiled from "HelloWorld.java"
public class HelloWorld
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #6.#15         // java/lang/Object."<init>":()V
   #2 = Fieldref           #16.#17        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = String             #18            // Hello World
   #4 = Methodref          #19.#20        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #5 = Class              #21            // HelloWorld
   #6 = Class              #22            // java/lang/Object
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               main
  #12 = Utf8               ([Ljava/lang/String;)V
  #13 = Utf8               SourceFile
  #14 = Utf8               HelloWorld.java
  #15 = NameAndType        #7:#8          // "<init>":()V
  #16 = Class              #23            // java/lang/System
  #17 = NameAndType        #24:#25        // out:Ljava/io/PrintStream;
  #18 = Utf8               Hello World
  #19 = Class              #26            // java/io/PrintStream
  #20 = NameAndType        #27:#28        // println:(Ljava/lang/String;)V
  #21 = Utf8               HelloWorld
  #22 = Utf8               java/lang/Object
  #23 = Utf8               java/lang/System
  #24 = Utf8               out
  #25 = Utf8               Ljava/io/PrintStream;
  #26 = Utf8               java/io/PrintStream
  #27 = Utf8               println
  #28 = Utf8               (Ljava/lang/String;)V
{
  public HelloWorld();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String Hello World
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 4: 0
        line 5: 8
}
SourceFile: "HelloWorld.java"

From the output, you can notice below lines.

 

  minor version: 0

  major version: 52

 

major version 52 refer Java SE 8, you can confirm the same from above table.

 

Since the output of javap command is verbose, you can grep for the major and minor versions using below commands.

Linux, Mac

javap -verbose HelloWorld.class | grep "major"

javap -verbose HelloWorld.class | grep "minor"

 

Windows

javap -verbose HelloWorld .class | findstr "major"

javap -verbose HelloWorld .class | findstr "minor"

$javap -verbose HelloWorld.class | grep "major"
  major version: 52
$
$
$javap -verbose HelloWorld.class | grep "minor"
  minor version: 0



You may like  

Interview Questions

Quick guide to OutOfMemoryError in Java

StackOverFlowError vs OutOfMemoryError in Java

Different ways to iterate over a map using for-each loop

How for-each or enhanced for loop works in Java?

How to solve UnsupportedClassVersionError in Java?

No comments:

Post a Comment