如何在Java运行的时候动态加载一个jar包到classpath里面
2个回答
展开全部
给你个例子自己看一下吧不会了再追问,注释基本都有了
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 import java.io.<img id="selectsearch-icon" src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/qb/select-search.png" alt="搜索">File;import java.lang.reflect.<a href="https://www.baidu.com/s?wd=Method&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank" class="baidu-highlight">Method</a>;import java.net.<a href="https://www.baidu.com/s?wd=URL&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank"class="baidu-highlight">URL</a>;import java.net.<a href="https://www.baidu.com/s?wd=URL&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank"class="baidu-highlight">URL</a><a href="https://www.baidu.com/s?wd=Class&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank" class="baidu-highlight">Class</a>Loader;import java.util.List; public final <a href="https://www.baidu.com/s?wd=class&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank"class="baidu-highlight">class</a> Ext<a href="https://www.baidu.com/s?wd=Class&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank" class="baidu-highlight">Class</a>pathLoader { private static <a href="https://www.baidu.com/s?wd=Method&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank" class="baidu-highlight">Method</a> add<a href="https://www.baidu.com/s?wd=URL&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank" class="baidu-highlight">URL</a> = initAdd<a href="https://www.baidu.com/s?wd=Method&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank" class="baidu-highlight">Method</a>(); private static URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); /** * <a href="https://www.baidu.com/s?wd=%E5%88%9D%E5%A7%8B%E5%8C%96&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ryc3nvD4mWT4rjnzmvw-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjRdP1mdPj6dn10dnWf3nHmd" target="_blank" class="baidu-highlight">初始化</a>addUrl 方法. * @return 可访问addUrl方法的Method对象 */ private static Method initAddMethod() { try { Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class }); add.setAccessible(true); return add; } catch (Exception e) { throw new RuntimeException(e); } } /** * 加载jar classpath。 */ public static void loadClasspath() { List<String> files = getJarFiles(); for (String f : files) { loadClasspath(f); } List<String> resFiles = getResFiles(); for (String r : resFiles) { loadResourceDir(r); } } private static void loadClasspath(String filepath) { File file = new File(filepath); loopFiles(file); } private static void loadResourceDir(String filepath) { File file = new File(filepath); loopDirs(file); } /** *//** * 循环遍历目录,找出所有的资源路径。 * @param file 当前遍历文件 */ private static void loopDirs(File file) { // 资源文件只加载路径 if (file.isDirectory()) { addURL(file); File[] tmps = file.listFiles(); for (File tmp : tmps) { loopDirs(tmp); } } } /** * 循环遍历目录,找出所有的jar包。 * @param file 当前遍历文件 */ private static void loopFiles(File file) { if (file.isDirectory()) { File[] tmps = file.listFiles(); for (File tmp : tmps) { loopFiles(tmp); } } else { if (file.getAbsolutePath().endsWith(".jar") || file.getAbsolutePath().endsWith(".zip")) { addURL(file); } } } /** * 通过filepath加载文件到classpath。 * @param filePath 文件路径 * @return URL * @throws Exception 异常 */ private static void addURL(File file) { try { addURL.invoke(classloader, new Object[] { file.toURI().toURL() }); } catch (Exception e) { } } /*** * 从配置文件中得到配置的需要加载到classpath里的路径集合。 * @return */ private static List<String> getJarFiles() { // TODO 从properties文件中读取配置信息 如果不想配置 可以自己new 一个List<String> 然后把 jar的路径加进去 然后返回 return null; } /** * 从配置文件中得到配置的需要加载classpath里的资源路径集合 * @return */ private static List<String> getResFiles() { //TODO 从properties文件中读取配置信息略 如果不想配置 可以自己new 一个List<String> 然后把 jar的路径加进去 然后返回 额 如果没有资源路径为空就可以了 return null; } public static void main(String[] args) { ExtClasspathLoader.loadClasspath(); }}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询