Java 動態(tài)加載jar和class文件實例解析
本文研究的主要是Java 動態(tài)加載jar和class文件的相關(guān)內(nèi)容,具體如下。
JAVA中類文件加載是動態(tài)的。也就是說當(dāng)我們用到的時候才會去加載,如果不用的話,就不會去加載我們的類。
JAVA為我們提供了兩種動態(tài)機(jī)制。第一種是隱式機(jī)制。第二種是顯示機(jī)制。如下:
兩種方法:
- 隱式機(jī)制 :new一個對象 + 調(diào)用類的靜態(tài)方法
- 顯式機(jī)制 :由 java.lang.Class的forName()方法加載
由 java.lang.ClassLoader的loadClass()方法加載
1、Class.forName
Class.forName()方法具有兩個形式:
public static Class forName(String className)public static Class forName(String className, boolean initialize,ClassLoader loader)
參數(shù)說明:
- className - 所需類的完全限定名 (必須包含包名,否則出錯!)
- initialize - 是否必須初始化類 (靜態(tài)代碼塊的初始化)
- loader - 用于加載類的類加載器
調(diào)用只有一個參數(shù)的forName()方法等效于 Class.forName(className, true, loader)。
這兩個方法,最后都要連接到原生方法forName0().
而三個參數(shù)的forName(),最后調(diào)用的是: forName0(name, initialize, loader);
不管使用的是new 來實例化某個類、或是使用只有一個參數(shù)的Class.forName()方法,內(nèi)部都隱含了“載入類 + 運(yùn)行靜態(tài)代碼塊”的步驟。
而使用具有三個參數(shù)的Class.forName()方法時,如果第二個參數(shù)為false,那么類加載器只會加載類,而不會初始化靜態(tài)代碼塊,只有當(dāng)實例化這個類的時候,靜態(tài)代碼塊才會被初始化,靜態(tài)代碼塊是在類第一次實例化的時候才初始化的。
2、java.lang.ClassLoader
ClassLoader就是用來Load Class的,當(dāng)一個Class被加載的時候,這個Class所引用到的所有Class也會被加載,而且這種加載是遞歸的,也就是說,如果A引用到B,B 引用到C,那么當(dāng)A被加載的時候,B也會被加載,而B被加載的時候,C也會加載。如此遞歸直到所有需要的Class都加載好。
package com.demo.test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
public class DynamicLoadDemo {
enum FileType {
JAR, CLASS, OTHER
}
static class MyClassLoader extends ClassLoader {
public synchronized Class<?> loadClass(String name, File file) throws FileNotFoundException {
Class<?> cls = findLoadedClass(name);
if(cls != null) {
return cls;
}
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
try {
while (true) {
len = fis.read(buffer);
if (len == -1) {
break;
}
baos.write(buffer, 0, len);
}
//FileInputStream的flush是空操作,因為flush的作用是把緩存中的東西寫入實體(硬盤或網(wǎng)絡(luò)流)中,這里沒有這種必要所以為空
//baos.flush();
byte[] data = baos.toByteArray();
return defineClass(null, data, 0, data.length);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
baos.close();
}
catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
public static void main(String[] args) {
String className = "com.demo.test.HelloWorld";
String paths[] = { "HelloWorld.jar", "HelloWorld.class" };
for (String path : paths) {
String lowerPath = path.toLowerCase();
FileType fileType = FileType.OTHER;
if (lowerPath.endsWith(".jar") || lowerPath.endsWith(".zip")) {
fileType = FileType.JAR;
} else if (lowerPath.endsWith(".class")) {
fileType = FileType.CLASS;
}
if (fileType == FileType.OTHER) {
return;
}
File file = new File(path);
if (!file.exists()) {
return;
}
try {
URL url = file.toURI().toURL();
System.out.println(url.toString());
Class<?> cls = null;
switch (fileType) {
case JAR:
URLClassLoader classLoader = new URLClassLoader(new URL[] { url }, Thread.currentThread().getContextClassLoader());
cls = classLoader.loadClass(className);
break;
case CLASS:
MyClassLoader myClassLoader = new MyClassLoader();
cls = myClassLoader.loadClass(className, file);
break;
default:
break;
}
if (cls == null) {
return;
}
// 實例變量
Field field = cls.getDeclaredField("hello");
if (!field.isAccessible()) {
field.setAccessible(true);
}
System.out.println(field.get(cls.newInstance()));
// 調(diào)用靜態(tài)不帶參數(shù)方法
Method staticMethod = cls.getDeclaredMethod("sayStaticHello", null);
if (!staticMethod.isAccessible()) {
staticMethod.setAccessible(true);
}
// 如果函數(shù)的返回值是void,就會返回null
staticMethod.invoke(cls, null);
// 實例帶參數(shù)方法方法
Method method = cls.getDeclaredMethod("say", String.class);
if (!method.isAccessible()) {
method.setAccessible(true);
}
Object ret = method.invoke(cls.newInstance(), "hello world");
System.out.println(ret);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (SecurityException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
結(jié)果:

總結(jié)
以上就是本文關(guān)于Java 動態(tài)加載jar和class文件實例解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
Java?ArrayList遍歷foreach與iterator時remove的區(qū)別
這篇文章主要介紹了Java?ArrayList遍歷foreach與iterator時remove的區(qū)別,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
Activiti進(jìn)階之組任務(wù)實現(xiàn)示例詳解
這篇文章主要為大家介紹了Activiti進(jìn)階之組任務(wù)實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
java數(shù)學(xué)工具類Math詳解(round方法)
這篇文章主要為大家詳細(xì)介紹了java數(shù)學(xué)工具類Math,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08

