java查看class_查看java class文件版本的方法

引用

http://www.rgagnon.com/javadetails/java-0544.html

The first 4 bytes are a magic number, 0xCAFEBABe, to identify a valid class file then the next 2 bytes identify the class format version (major and minor).

Possible major/minor value :

major  minor Java platform version

45       3           1.0

45       3           1.1

46       0           1.2

47       0           1.3

48       0           1.4

49       0           1.5

50       0           1.6

import java.io.*;

public class ClassVersionChecker {

public static void main(String[] args) throws IOException {

for (int i = 0; i < args.length; i++)

checkClassVersion(args[i]);

}

private static void checkClassVersion(String filename)

throws IOException

{

DataInputStream in = new DataInputStream

(new FileInputStream(filename));

int magic = in.readInt();

if(magic != 0xcafebabe) {

System.out.println(filename + " is not a valid class!");;

}

int minor = in.readUnsignedShort();

int major = in.readUnsignedShort();

System.out.println(filename + ": " + major + " . " + minor);

in.close();

}

}

> java ClassVersionChecker ClassVersionChecker.class

ClassVersionChecker.class: 49 . 0

from The Java Virtual Machine Specification

magic

The magic item supplies the magic number identifying the class file format; it has the value 0xCAFEBABE.

minor_version, major_version

The values of the minor_version and major_version items are the minor and major version numbers of this class file.Together, a major and a minor version number determine the version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m. Thus, class file format versions may be ordered lexicographically, for example, 1.5 < 2.0 < 2.1.

A Java virtual machine implementation can support a class file format of version v if and only if v lies in some contiguous range Mi.0 v Mj.m. Only Sun can specify what range of versions a Java virtual machine implementation conforming to a certain release level of the Java platform may support.

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2008-11-07 16:36

浏览 5410

论坛回复 / 浏览 (0 / 7246)

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值