首页

关于JarManagerUtil包管理工具类进行jar加载及获取其class对象集等相关操作代码示例

标签:JarManagerUtil,jar工具类,加载JAR初始化     发布时间:2018-10-31   

一、前言

通过java.util.jar.JarFile类定义JarManagerUtil管理工具类,进行指定文件loadJar加载、获取指定文件class对象集(getClazzInstances)、获取jar包文件文件getInputStream数据流等操作,详情代码示例说明。

二、代码示例

import java.io.File;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.net.URL;@b@import java.net.URLClassLoader;@b@import java.util.ArrayList;@b@import java.util.Enumeration;@b@import java.util.HashMap;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.jar.JarEntry;@b@import java.util.jar.JarFile;@b@@b@public class JarManagerUtil {@b@@b@	public static Map<String,Class<?>> jarClasses = new HashMap<String,Class<?>>();@b@@b@@b@	/**@b@	 * 加载.jar文件对象实体@b@	 * @param file 传入一个.jar文件,获取.jar文件对象实体@b@	 * @return@b@	 */@b@	public static void loadJar(File file) {@b@		try {@b@			if (file == null)@b@				return;@b@			URLClassLoader loader = new URLClassLoader(new URL[] { new URL(@b@					"file:" + file.getAbsolutePath()) }, Thread.currentThread()@b@					.getContextClassLoader());@b@			JarFile jarFile = new JarFile(file);@b@			Enumeration<?> enum1 = jarFile.entries();@b@			while (enum1.hasMoreElements()) {@b@				JarEntry entry = (JarEntry) enum1.nextElement();@b@				if (entry.isDirectory())@b@					continue;@b@				String jar = entry.getName();@b@				if (jar.endsWith(".class")) {@b@					String clzname = jar.substring(0, jar.lastIndexOf(".class")).replace('/', '.');@b@					Class<?> clz = loader.loadClass(clzname);@b@					jarClasses.put(clzname, clz);@b@				}@b@			}@b@			if(jarFile != null) jarFile.close();@b@		} catch (Exception e) {@b@			e.printStackTrace();@b@		}@b@	}@b@	@b@	/**@b@	 * @param file 传入一个.jar文件,获取.jar文件对象实体@b@	 * @return@b@	 */@b@	public static List<Class<?>> getClazzInstances(File file) {@b@		try {@b@			if (file == null)@b@				return null;@b@			URLClassLoader loader = new URLClassLoader(new URL[] { new URL(@b@					"file:" + file.getAbsolutePath()) }, Thread.currentThread()@b@					.getContextClassLoader());@b@			List<Class<?>> classinstances = new ArrayList<Class<?>>();@b@			JarFile jarFile = new JarFile(file);@b@			Enumeration<?> enum1 = jarFile.entries();@b@			while (enum1.hasMoreElements()) {@b@				JarEntry entry = (JarEntry) enum1.nextElement();@b@				if (entry.isDirectory())@b@					continue;@b@				String jar = entry.getName();@b@				if (jar.endsWith(".class")) {@b@					String clzname = jar.substring(0, jar.lastIndexOf(".class")).replace('/', '.');@b@					Class<?> clz = loader.loadClass(clzname);@b@					classinstances.add(clz);@b@					jarClasses.put(clzname, clz);@b@				}							@b@			}@b@			if(jarFile != null) jarFile.close();@b@			return classinstances;@b@		} catch (Exception e) {@b@			e.printStackTrace();@b@		}@b@		return null;@b@	}@b@	@b@	/**@b@	 * 获取属性文件实例@b@	 * @param suffix TODO@b@	 * @b@	 */@b@	public static InputStream getInputStream(File file, String suffix) {@b@		InputStream is=null;@b@		try {@b@			if (file == null)@b@				return null;@b@			JarFile jarFile = new JarFile(file);@b@			Enumeration<?> enum1 = jarFile.entries();@b@			while (enum1.hasMoreElements()) {@b@				JarEntry entry = (JarEntry) enum1.nextElement();@b@				if (entry.isDirectory())@b@					continue;@b@				String jar = entry.getName();@b@				if(jar.endsWith(suffix)) {					@b@					is= jarFile.getInputStream(entry);@b@				}				@b@			}@b@			if(jarFile != null) jarFile.close();@b@			return is;@b@		} catch (Exception e) {@b@			e.printStackTrace();@b@		}@b@		return is;@b@	}@b@	@b@@b@	/**@b@	 * 判断目录中是否已存在相同名字的文件@b@	 */@b@	public static boolean existsSameJar(String path,String filename){@b@		File file = new File(path);@b@		String jarPath = file.getPath()+"/"+filename;@b@		File jar = new File(jarPath);@b@		return jar.exists();@b@	}@b@@b@	/**@b@	 * 在传入路径中创建文件@b@	 * @param data@b@	 * @param filename@b@	 * @return@b@	 */@b@	public static File createTempFile(byte[] data,String filename,String path){@b@		FileOutputStream fos = null;@b@		path = path+"/temp";@b@		File f=new File(path);@b@		if(!f.exists()){@b@			f.mkdirs();@b@		}@b@		try{@b@			fos = new FileOutputStream(f.getPath()+'/'+filename);@b@			fos.flush();@b@			fos.write(data);@b@			return new File(path+'/'+filename);@b@		}catch(Exception e1){@b@			e1.printStackTrace();@b@		}finally{@b@			try {@b@				if (fos != null) fos.close();@b@			} catch (IOException e) {@b@				e.printStackTrace();@b@			}@b@		}@b@		return null;@b@	}@b@	@b@	@b@}