프로그래머의 삶 Programmer's Life/Java!!

자바 클래스파일 컴파일 버젼 알아내기

Oliver's World 2012. 3. 23. 13:48
728x90


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Java Version Check Program
 *
 * by lvsin 2012/03/23
 */
public class VersionChk {
 public static void main(String[] args) throws IOException {
  checkClassVersion("a.class");
 }

 public static void checkClassVersion(String filename) {
  String script = " - java compile version is :  ";
  try {
   DataInputStream in = new DataInputStream(new FileInputStream(
     filename));
   int magic = in.readInt();
   if (magic != 0xcafebabe) {
    System.out.println(filename + " is not a valid calss!");
   }
   int minor = in.readUnsignedShort();
   int major = in.readUnsignedShort();

   in.close();

   if (major == 45 && minor == 3) {
    System.out.println(filename + script + "1.1");
   } else if (major == 46 && minor == 0) {
    System.out.println(filename + script + "1.2");
   } else if (major == 47 && minor == 0) {
    System.out.println(filename + script + "1.3");
   } else if (major == 48 && minor == 0) {
    System.out.println(filename + script + "1.4");
   } else if (major == 49 && minor == 0) {
    System.out.println(filename + script + "1.5");
   } else if (major == 50 && minor == 0) {
    System.out.println(filename + script + "1.6");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

728x90