编译class文件的jdk版本

仿造网上搜索到的例子,写了个查看class文件的工具类,以后万一报错

[ERROR]20/10/2008 14:17:59,765 (org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/spring]): Error configuring application listener of class com.example.controller.ServiceInitializer
Exception: -Bad version number in .class file-
-31- java.lang.UnsupportedClassVersionError: Bad version number in .class file

就可以用这个工具检查一下

package com.sillycat.api.commons.utils;

import java.io.FileInputStream;

public class JavaVersionUtil {

private static final String str = "E:/book/rest/spring/WEB-INF/classes/com/example/business/local/TopicManagerImpl.class";

// 版本号对应:
// 5.0
// 版本号(version):49.0
// 6.0
// 版本号(version):50.0
// 1.4
// 版本号(version):46.0
// 1.3
// 版本号(version):45.3

public static void main(String args[]) {
   try {
    // 读取文件数据,文件是当前目录下的First.class
    FileInputStream fis = new FileInputStream(str);
    int length = fis.available();
    // 文件数据
    byte[] data = new byte[length];
    // 读取文件到字节数组
    fis.read(data);
    // 关闭文件
    fis.close();
    // 解析文件数据
    parseFile(data);
   } catch (Exception e) {
    System.out.println(e);
   }
}

private static void parseFile(byte[] data) {
   // 输出魔数
   System.out.print("魔数(magic):0x");
   System.out.print(Integer.toHexString(data[0]).substring(6)
     .toUpperCase());
   System.out.print(Integer.toHexString(data[1]).substring(6)
     .toUpperCase());
   System.out.print(Integer.toHexString(data[2]).substring(6)
     .toUpperCase());
   System.out.println(Integer.toHexString(data[3]).substring(6)
     .toUpperCase());
   // 主版本号和次版本号码
   int minor_version = (((int) data[4]) << + data[5];
   int major_version = (((int) data[6]) << + data[7];
   System.out.println("版本号(version):" + major_version + "."
     + minor_version);
}

}

你可能感兴趣的:(apache,spring,jdk,Web,REST)