CLASS版本检测

因为之前在试用jxl的时候,下载jexcelapi_2_6_12.zip运行报java.lang.UnsupportedClassVersionError,怀疑是jar的版本太高,但是通过MENIFEST.MF“Created-By: 11.0-b15 (Sun Microsystems Inc.)”这类信息,所以特编写一个类版本检测工具:

 

1、相关代码如下:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ClassVersionChecker {
	static Map<Integer, String> versionMap = new HashMap<Integer, String>();
	static {
		versionMap.put(Integer.valueOf(46), "1.2");
		versionMap.put(Integer.valueOf(47), "1.3");
		versionMap.put(Integer.valueOf(48), "1.4");
		versionMap.put(Integer.valueOf(49), "1.5");
		versionMap.put(Integer.valueOf(50), "1.6");
	}

	public static void main(String[] args) throws IOException {
		checkClassVersion("classes/Cell.class");
	}

	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 + " jdk:" + versionMap.get(major));
		in.close();
	}
}

 

2、输出如下:

classes/Cell.class: 50.0 jdk:1.6

 

3、下面提供一份标准的对照表:

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

你可能感兴趣的:(java,jdk,sun)