JarFile解析jar文件。获取jar中后缀为class文件路径

解析jar文件。获取jar中。后缀为class文件的全限定名。而且判断。该class文件。是否继承和实现某个接口。
以下是测试类

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.jar.*;
public class JarDir {
  public static void main (String args[]) 
    throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
   
	List size=new ArrayList();
  
	//JarFile jarFile = new JarFile("D:/projectList2017_10_24reconciliation_ba_pageconfig/reconciliation_ba/target/classes/deploy/1311-v1.0.0.jar");
    String des="C:/Users/zhangrui/Desktop/对账系统";
    String namess="111-v1.0.0.jar";
    @SuppressWarnings("resource")
    JarFile jarFile = new JarFile(des+ File.separator+namess);
    File file=new File(des+File.separator+namess);
    URL url=file.toURI().toURL(); 
    ClassLoader  loader=new URLClassLoader(new URL[]{url});
    
    //"C:/Users/zhangrui/Desktop/对账系统/131-v1.0.0.jar"
    Enumeration files  = jarFile.entries();
    while (files .hasMoreElements()) {
      process(files .nextElement(),size,loader);
    }
    System.out.println(size.toString());
  }
  
  
 private static void process(Object obj,List size ,ClassLoader loader) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
	
    JarEntry entry = (JarEntry)obj;
    String name = entry.getName();
    
    //格式化
    formatName( size, name, loader);
  }
  
  
private static void formatName(List size, String name,ClassLoader loader) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {

	if(!name.endsWith(".MF")){
    	if(name.endsWith(".class")){
    		String d=name.replaceAll("/",".");
    		int n=6;
    		//第一个参数是开始截取的位置,第二个是结束位置。 
			String names=d.substring(0,name.length()-n);
			 /*动态加载指定jar包调用其中某个类的方法*/  
	        Class cls=loader.loadClass(names);//加载指定类,注意一定要带上类的包名  
	        Class  lifeCycle= com.qbao.pay.balance.core.deploy.LifeCycle.class; 
	        System.out.println(names+"="+lifeCycle.isAssignableFrom(cls));
	        if(lifeCycle.isAssignableFrom(cls)){
	        	size.add(names);
	        }
	        
    	}
	
	}
}
}


以下是程序类方法

@Override
	protected List unpackSoFile(File objFile,
			String directory) throws Exception {
		//jar组件清单集合
		List jarList=new ArrayList();
		
		//创建jarFile实例
		@SuppressWarnings("resource")
		JarFile jarFile = new JarFile(directory+ File.separator+objFile);
		
		File file=new File(directory+File.separator+objFile);
	    URL url=file.toURI().toURL(); 
	    //类加载器
	    ClassLoader  loader=new URLClassLoader(new URL[]{url});
		
		//JarFile的entries方法返回所有entries的枚举集合(Enumeration)。通过每一个entry,你可以从它的manifest文件得到它的属性,
		Enumeration fileList = jarFile.entries();
		
		//hasMoreElements()方法用来判断Enumeration接口中是否还有元素,返回一个boolean值。
		while (fileList .hasMoreElements()) {
			//获取元素条目信息
		      process(fileList .nextElement(),jarList,loader);
		    }
		return jarList;
	}

	public void process(Object fileList,List jarList ,ClassLoader loader){
		// 为指定的 JAR 文件条目名称创建新的 JarEntry。
		JarEntry entry = (JarEntry)fileList;
		
		//返回条目名称。
	    String name = entry.getName();
	    
	    //格式化条目名称
	    formatName(jarList, name , loader);
		
	}
	/**
	 * 格式化jar清单路径(条目名称)
	 * @param size
	 * @param name
	 */
	private  void formatName(List size, String name,ClassLoader loader) {
		if(!name.endsWith(".MF")){
	    	if(name.endsWith(".class")){
	    		String fileName=name.replaceAll("/",".");
	    		int n=6;
				String ultimaName=fileName.substring(0,name.length()-n);
				try {
					//加载指定类,注意一定要带上类的包名 
					Class cls= loader.loadClass(ultimaName);
				    Class  lifeCycle= com.qbao.pay.balance.core.deploy.LifeCycle.class; 
				    
				    if(lifeCycle.isAssignableFrom(cls)){
				        	size.add(ultimaName);
				     }
	    		
		    	} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
		    }
		}
	}



你可能感兴趣的:(java)