判断jar包中class文件的java编译版本

该Java程序用于检测jar包中class文件的Java编译版本。通过读取文件的前4字节魔数0xCAFEBABE来验证是否为有效的class文件,并进一步读取后续2字节确定主次版本号。代码包含多个方法,如checkClassVersion()、scanJarFileJDK1_5_Version()等,分别用于检查单个文件、jar包内的文件是否符合Java 1.5及以上版本。
摘要由CSDN通过智能技术生成

  

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
//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 :
//create table m2_lib ( library1 varchar2(100)) ;
//select '"'||replace(library1,'/','/')||'",' from m2_lib   ;
//
//select * from    m2_lib  for update;
//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

public class ClassVersionChecker {
 public static void main(String[] args) throws IOException {
  // for (int i = 0; i < args.length; i++)
  // checkClassVersion(args[i]);
 
  String path = "d://aaa/";
  File file = new File(path);
 
  File [] jarsStrings =  file.listFiles();
  for (int i = 0; i < jarsStrings.length; i++)
   scanJarFileJDK1_5_Version(jarsStrings[i].getAbsolutePath());
 }
 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();
 }
 private static void checkClassVersion(InputStream in1) throws IOException {
  DataInputStream in = new DataInputStream(in1);
  int magic = in.readInt();
  if (magic != 0xcafebabe) {
   System.out.println(in + " is not a valid class!");
   ;
  }
  int minor = in.readUnsignedShort();
  int major = in.readUnsignedShort();
 
  System.out.println(": " + major + " . " + minor);
  in.close();
 }
 
 private static void checkClassJDK1_5_Version(String jar,InputStream in1) throws IOException {
  DataInputStream in = new DataInputStream(in1);
  int magic = in.readInt();
  if (magic != 0xcafebabe) {
   System.out.println(in + " is not a valid class!");
   ;
  }
  int minor = in.readUnsignedShort();
  int major = in.readUnsignedShort();
  if (major >49)
  System.out.println(jar+": " + major + " . " + minor);
  in.close();
 }
 
 
 public static void scanJarFileJDK1_5_Version(String zipname) {
  try {
   ZipInputStream zin = new ZipInputStream(
     new FileInputStream(zipname));
   ZipEntry entry;
   while ((entry = zin.getNextEntry()) != null) {
    if (entry.getName().endsWith(".class")) {
     ZipFile file = new ZipFile(zipname);
     InputStream in = file.getInputStream(entry);
     checkClassJDK1_5_Version(zipname,in);
     break;
    }
   }
   zin.close();
  } catch (IOException e) {
  }
 }
 
 public static void scanZipFile(String zipname) {
  try {
   ZipInputStream zin = new ZipInputStream(
     new FileInputStream(zipname));
   ZipEntry entry;
   while ((entry = zin.getNextEntry()) != null) {
    System.out.println(entry.getName());
    zin.closeEntry();
   }
   zin.close();
  } catch (IOException e) {
  }
 }
 public static void scanClassFile(String zipname) {
  try {
   ZipInputStream zin = new ZipInputStream(
     new FileInputStream(zipname));
   ZipEntry entry;
   while ((entry = zin.getNextEntry()) != null) {
    if (entry.getName().endsWith(".class")) {
     System.out.println(entry.getName());
     ZipFile file = new ZipFile(zipname);
     InputStream in = file.getInputStream(entry);
     System.out.println(in);
     checkClassVersion(in);
     break;
    }
   }
   zin.close();
  } catch (IOException e) {
  }
 }
 public static void loadZipFile(String zipname, String name) {
  try {
   ZipInputStream zin = new ZipInputStream(
     new FileInputStream(zipname));
   ZipEntry entry;
   System.out.println("");
   while ((entry = zin.getNextEntry()) != null) {
    if (entry.getName().equals(name)) {
     BufferedReader in = new BufferedReader(
       new InputStreamReader(zin));
     String s;
     while ((s = in.readLine()) != null)
      System.out.println(s + "/n");
    }
    zin.closeEntry();
   }
   zin.close();
  } catch (IOException e) {
  }
 }
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值