码迷,mamicode.com
首页 > 其他好文 > 详细

打印class文件的版本号

时间:2016-05-15 13:56:42      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

 

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

public class ClassVersionChecker {
    public static void main(String[] args) throws IOException {
        System.out.println(System.getProperty("java.version"));
        String canonicalName = ClassVersionChecker.class.getCanonicalName();
        String classPathSeparate = "/";
        String extension = ".class";
        String path = ClassVersionChecker.class.getResource(classPathSeparate + canonicalName.replace(".", classPathSeparate) + extension).getPath();
        File file = new File(path);
        checkClassVersion(file);
    }

    private static void checkClassVersion(File classFile)
            throws IOException {
        DataInputStream in = new DataInputStream
                (new FileInputStream(classFile));

        int magic = in.readInt();
        if (magic != 0xcafebabe) {
            System.out.println(classFile.getName() + " is not a valid class!");
        }
        int minor = in.readUnsignedShort();
        int major = in.readUnsignedShort();
        System.out.println(classFile.getName() + ": " + major + " . " + minor);
        in.close();
    }
} 

 

JDK1.8:

1.8.0_65
ClassVersionChecker.class: 52 . 0

 

JDK1.61.6.0_45
ClassVersionChecker.class: 50 . 0

 

打印class文件的版本号

标签:

原文地址:http://www.cnblogs.com/softidea/p/5495065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!