当前位置: 首页 > 工具软件 > Wiki in a Jar > 使用案例 >

jar包检索

司徒捷
2023-12-01

项目组需要将代码中所有的jar包检索出来,并对jar中内容进行分析,特开发出这个java小工具,同样这也是一个模板,可以按照需要修改任何想要检索的文件!想要了解对jar中内容分析的代码如何编写可以给我留言欧。

import java.io.*;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class FindClass{
	private static int num = 0;
	private static File projectRoot = new File("文件路径");
	private static void main(String[] args) throws IOException {
		findSpecifiedClassInJar(projectRoot);
	}
	public static boolean findClassInJar(File file) throws IOException {
		if(file.isDirectory()){
			for(File subFile : file.listFiles()){
				if(findClassInJar(subFile)){
					return true;
				}
			}
		}else{
			if(file.getAbsolutePath().endsWith(".jar")){
				try{
					JarFile jarFile = new JarFile(file);
					Enumeration<JarEntry> enu = jarFile.entries();
					System.out.println(file.getName());
					num++;
					while(enu.hasMoreElements()){
						JarEntry jarEntry = enu.nextElment();
						String entry = jarEntry.getName();
						if(entry.endsWith(".class")){
							entry = entry.replaceAll("/",".").substring(0);
							System.out.println(entry);
							FileWriter fileWriter = new FileWriter("class.txt",true);
							BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
							bufferedWriter.write(entry);
							bufferedWriter.newLine();
							bufferedWriter.close();
						}
					}
				}catch (Exception e){
					
				}
			}
		}
		return false;
	}
}

 类似资料: