---
title: "通过ClassLoader调用外部jar包"
author: "mmdev"
date: "2007-11-12T15:38:00Z"
tags: []
slug: "classloader-jar"
draft: false
lastmod: "2012-04-14T20:14:29Z"
id: 1486377
url: "https://mmdev.iteye.com/blog/1486377"
---

我们大家都知道，每个运行中的线程都有一个成员contextClassLoader，用来在运行时动态地载入其它类。

系统默认的contextClassLoader是systemClassLoader，所以一般而言java程序在执行时可以使用JVM自带的类、$JAVA\_HOME/jre/lib/ext/中的类和$CLASSPATH/中的类，对于非默认的jar，一般只能手动在配置环境添加。

但事实上，我们可以通过`Thread.currentThread().setContextClassLoader()`更改当前线程的contextClassLoader行为，实现在程序内加载外部jar。

PS:  
ClassLoader的工作原理是：  
1\) 线程需要用到某个类时，contextClassLoader将被请求来载入该类  
2\) contextClassLoader请求它的父ClassLoader来完成该载入请求  
3\) 如果父ClassLoader无法载入类，则contextClassLoader试图自己来载入

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.loon.framework.jar;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Title:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Description:JarLoader，用于jar包的外部操作&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Copyright:Copyright(c)2007&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;Company:LoonFramework&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.BufferedInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.ByteArrayInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.ByteArrayOutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.FileInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.FileNotFoundException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.InputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.lang.reflect.Method;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.net.MalformedURLException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.net.URL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.ArrayList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.HashMap;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.Hashtable;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.jar.JarEntry;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.jar.JarInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.jar.Manifest;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassJarLoaderextendsClassLoader...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//资源缓存  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticHashtableresources=newHashtable();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticJarLoaderloader=newJarLoader();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticClassload(byte\[]resource)throwsException...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//主函数所在类全称  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StringmainClassName="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//class资源及实体缓存  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ArrayListclassNames=newArrayList();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ArrayListclassBuffers=newArrayList();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//存储依赖类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)HashMapdepends=newHashMap();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//将byte\[]转为JarInputStream  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JarInputStreamjar=newJarInputStream(newByteArrayInputStream(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)resource));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Manifestmanifest=jar.getManifest();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//当Main-Class被声明时,获得主函数所在类全称  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(manifest!=null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)mainClassName=manifest.getMainAttributes().getValue("Main-Class");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//依次获得对应JAR文件中封装的各个被压缩文件的JarEntry  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JarEntryentry;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while((entry=jar.getNextJarEntry())!=null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//当找到的entry为class时  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(entry.getName().toLowerCase().endsWith(".class"))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//将类路径转变为类全称  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringname=entry.getName().substring(0,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)entry.getName().length()-".class".length()).replace(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)'/','.');  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//加载该类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byte\[]data=getResourceData(jar);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//缓存类名及数据  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)classNames.add(name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)classBuffers.add(data);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}else...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//非class结尾但开头字符为'/'时  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(entry.getName().charAt(0)=='/')...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)resources.put(entry.getName(),getResourceData(jar));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//否则追加'/'后缓存  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}else...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)resources.put("/"+entry.getName(),getResourceData(jar));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//当获得的main-class名不为空时  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(classNames.size()&gt;0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//获得类路径全长  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intn=classNames.size();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inti=classNames.size()-1;i&gt;=0;i--)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//查询指定类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)loader.defineClass((String)classNames.get(i),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)(byte\[])classBuffers.get(i),0,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)((byte\[])classBuffers.get(i)).length);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//获得类名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StringpkName=(String)classNames.get(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(pkName.lastIndexOf('.')&gt;=0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pkName=pkName  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif).substring(0,pkName.lastIndexOf('.'));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(loader.getPackage(pkName)==null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)loader.definePackage(pkName,null,null,null,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)null,null,null,null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//查询后删除缓冲  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)classNames.remove(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)classBuffers.remove(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(NoClassDefFoundErrore)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)depends.put((String)classNames.get(i),e.getMessage()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif).replaceAll("/","."));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(UnsupportedClassVersionErrore)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//jre版本错误提示  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thrownewUnsupportedClassVersionError(classNames.get(i)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)+","+System.getProperty("java.vm.name")+""  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)+System.getProperty("java.vm.version")+")");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(n==classNames.size())...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inti=0;i&lt;classNames.size();i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.err.println("NoClassDefFoundError:"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)+classNames.get(i));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StringclassName=(String)classNames.get(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(depends.containsKey(className))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)className=(String)depends.get(className);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//加载  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Thread.currentThread().setContextClassLoader(loader);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//获得指定类,查找其他类方式相仿  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnClass.forName(mainClassName,true,loader);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(ClassNotFoundExceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StringclassName=mainClassName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(depends.containsKey(className))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)className=(String)depends.get(className);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thrownewClassNotFoundException(className);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*获得指定路径文件的byte\[]形式  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramname  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)finalstaticpublicbyte\[]getDataSource(Stringname)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)FileInputStreamfileInput;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fileInput=newFileInputStream(newFile(name));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(FileNotFoundExceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fileInput=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)BufferedInputStreambufferedInput=newBufferedInputStream(fileInput);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returngetDataSource(bufferedInput);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*获得指定InputStream的byte\[]形式  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramname  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)finalstaticpublicbyte\[]getDataSource(InputStreamis)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(is==null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnnull;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ByteArrayOutputStreambyteArrayOutputStream=newByteArrayOutputStream();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byte\[]arrayByte=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byte\[]bytes=newbyte\[8192];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bytes=newbyte\[is.available()];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intread;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while((read=is.read(bytes))&gt;=0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byteArrayOutputStream.write(bytes,0,read);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)arrayByte=byteArrayOutputStream.toByteArray();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(IOExceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnnull;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}finally...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(byteArrayOutputStream!=null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byteArrayOutputStream.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byteArrayOutputStream=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(is!=null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)is.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)is=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(IOExceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnarrayByte;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*获得指定JarInputStream的byte\[]形式  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramjar  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@throwsIOException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)finalstaticprivatebyte\[]getResourceData(JarInputStreamjar)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)throwsIOException...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ByteArrayOutputStreamdata=newByteArrayOutputStream();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byte\[]buffer=newbyte\[8192];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intsize;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(jar.available()&gt;0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)size=jar.read(buffer);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(size&gt;0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)data.write(buffer,0,size);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returndata.toByteArray();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*重载的getResource,检查是否重复包含  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicURLgetResource(Stringname)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(resources.containsKey("/"+name))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnnewURL("file:///"+name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(MalformedURLExceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnsuper.getResource(name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*执行指定class类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramclz  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@parammethodName  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramargs  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticvoidcallVoidMethod(Classclz,StringmethodName,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Class\[]arg=newClass\[1];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)arg\[0]=args.getClass();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Methodmethod=clz.getMethod(methodName,arg);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Object\[]inArg=newObject\[1];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inArg\[0]=args;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)method.invoke(clz,inArg);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(Exceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.err.println(e.getMessage());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*重载的getResourceAsStream,检查是否重复包含  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicInputStreamgetResourceAsStream(Stringname)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(name.charAt(0)=='/')...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)name=name.substring(1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(resources.containsKey("/"+name))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnnewByteArrayInputStream((byte\[])resources.get("/"+name));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnsuper.getResourceAsStream(name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

运行示例：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.loon.framework.jar;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:从外部启动jar包  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassJarTest...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//将jar包转为byte\[]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byte\[]resource=JarLoader.getDataSource("D:/fps\_test.jar");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//通过byte\[]获得主函数所在类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Classclz=JarLoader.load(resource);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//调用main函数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)JarLoader.callVoidMethod(clz,"main",newString\[]...{""});  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(Exceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.getStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

这时即使指定jar包没有被我们添加到lib中，程序依旧被顺利启动了。

但是有个缺点，在没有优化的前提下，这种直接加载外部包的速度在jvm会有很大损耗。  
![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/ttt.gif)  
我们可以看到，fps值降低到正常值的50%左右（此fps实例见我CSDN以前的文章），所以并不适合直接运用在需要复杂运算的jar中类调用上（当然，有兴趣的话，可以在代码中优化，我自己在项目中写的另一个例子速度明显比这个快）。但是对于运算量小的外部jar调用，还是很方便的。