检查java class的版本号

转自 http://blog.csdn.net/pandakong/article/details/9154277

补丁总是会一遍又一遍的打,越打越多

有时候,就担心有人不小心把高版本的class打到低版本jre运行的环境中

简单写了点代码,检查文件夹中class的版本号

  1. package org.wee.cv;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5.   
  6. public class ClassVersion {  
  7.       
  8.     /** 
  9.      * 检查class文件的版本号 
  10.      * @param classFile 
  11.      * @return 
  12.      * 返回值为:JDK1.4 JDK1.5 ... 或者unknown 
  13.      * @throws Exception 
  14.      */  
  15.     public static String checkClassVersion(File classFile) throws Exception{  
  16.         byte[] data = new byte[8];  
  17.           
  18.         FileInputStream in = new FileInputStream(classFile);  
  19.         //读取文件前8字节  
  20.         //实际上版本号写在第4-7字节上(从第0字节开始算)  
  21.         in.read(data, 08);  
  22.         in.close();  
  23.           
  24.         //计算出class文件的主次版本号  
  25.         int minor_version = (((int)data[4])<<8)+data[5];  
  26.         int major_version = (((int)data[6])<<8)+data[7];  
  27.         return translateVersionToJDK(major_version);  
  28.     }  
  29.       
  30.     /** 
  31.      * 根据主版本号,转换成JDK版本 
  32.      * 48是JDK1.4,49是JDK1.5,依次类推 
  33.      * @param major_version 
  34.      * @return 
  35.      */  
  36.     public static String translateVersionToJDK(final int major_version){  
  37.         switch(major_version){  
  38.         case 48:  
  39.             return "JDK1.4";  
  40.         case 49:  
  41.             return "JDK1.5";  
  42.         case 50:  
  43.             return "JDK1.6";  
  44.         case 51:  
  45.             return "JDK1.7";  
  46.         default:  
  47.             return "unknown";  
  48.         }  
  49.     }  
  50.   
  51. }  

 

    1. package org.wee.cv;  
    2.   
    3. import java.io.File;  
    4. import java.util.ArrayList;  
    5. import java.util.HashMap;  
    6. import java.util.List;  
    7.   
    8. public class BatchClassVersionCheck {  
    9.   
    10.     public static void main(String[] args) {  
    11.         try {  
    12.             BatchClassVersionCheck bcvc = new BatchClassVersionCheck();  
    13.             HashMap<String,List<String>> versionMap = bcvc.getDirectoryClassVersionInfo(new File("D:/test"));  
    14.             for (String version : versionMap.keySet()){  
    15.                 System.out.println("version:" + version);  
    16.                 List<String> list = versionMap.get(version);  
    17.                 for (String file : list){  
    18.                     System.out.println(file);  
    19.                 }  
    20.             }  
    21.         } catch (Exception e) {  
    22.             e.printStackTrace();  
    23.         }  
    24.   
    25.     }  
    26.       
    27.     //保存文件夹中的class文件版本信息  
    28.     //key是版本号  
    29.     //value是对应文件的绝对路径  
    30.     private HashMap<String,List<String>> classVersionInfoMap;  
    31.       
    32.     /** 
    33.      * 获取文件夹中class类的版本信息 
    34.      * @param dir 
    35.      * @return 
    36.      * @throws Exception 
    37.      */  
    38.     public HashMap<String,List<String>> getDirectoryClassVersionInfo(File dir) throws Exception{  
    39.         classVersionInfoMap = new HashMap<String,List<String>>();  
    40.         searchClass(dir);  
    41.         return classVersionInfoMap;  
    42.     }  
    43.       
    44.     /** 
    45.      * 递归方法 
    46.      * 搜索当前文件夹下的class文件,并计算版本信息,保存在map中 
    47.      * 当搜索到文件夹时,递归搜索 
    48.      * @param dir 
    49.      * @throws Exception 
    50.      */  
    51.     protected void searchClass(File  dir) throws Exception{  
    52.         File[] childFiles = dir.listFiles();  
    53.         for (File childFile : childFiles){  
    54.             if (childFile.isDirectory()){  
    55.                 //递归搜索子文件夹  
    56.                 searchClass(childFile);  
    57.             } else{  
    58.                 if (childFile.getName().toLowerCase().endsWith(".class")){  
    59.                     //搜索出class文件  
    60.                     //将版本信息记录在map中  
    61.                     putVersionInfo(ClassVersion.checkClassVersion(childFile), childFile.getAbsolutePath());  
    62.                 }  
    63.             }  
    64.         }  
    65.     }  
    66.       
    67.     /** 
    68.      * 将版本信息记录在map中 
    69.      * @param version 
    70.      * @param absolutePath 
    71.      */  
    72.     private void putVersionInfo(String version,String absolutePath){  
    73.         List<String> list = null;  
    74.         if (classVersionInfoMap.containsKey(version)){  
    75.             list = classVersionInfoMap.get(version);  
    76.         } else{  
    77.             list = new ArrayList<String>();  
    78.         }  
    79.         list.add(absolutePath);  
    80.         classVersionInfoMap.put(version, list);  
    81.     }  
    82.   

posted @ 2014-01-02 16:13  princessd8251  阅读(212)  评论(0)    收藏  举报