Check the class version by readin first int from a class file in Java

Description

The following code shows how to check the class version by readin first int from a class file.

Example


// w  w  w  .  j  ava  2  s .  c om
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    DataInputStream in = new DataInputStream(new FileInputStream("Main.class"));

    int start = in.readInt();
    if (start != 0xcafebabe) {
      System.out.println("not valid");
    }
    in.close();
    System.out.println(in.readUnsignedShort()+"/"+in.readUnsignedShort());
    
  }
}




















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy