public class CustomClassLoader extends ClassLoader {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
System.out.println(getSystemClassLoader());
String path = this.getClass().getResource("").getPath();
File file = new File(path.substring(0, path.indexOf("class") + 8) + name.replaceAll("\\.", "/") + ".class");
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
InputStreamReader fileInputStream = new InputStreamReader(new FileInputStream(file), StandardCharsets.ISO_8859_1);
// FileInputStream fileInputStream = new FileInputStream(file);
int b = 0;
while ((b = fileInputStream.read()) !=0) {
byteArrayOutputStream.write(b);
}
byteArrayOutputStream.close();
fileInputStream.close();
byte[] bytes = byteArrayOutputStream.toByteArray();
String s = new String(bytes);
System.out.println(s);
return super.defineClass(name, bytes, 0, bytes.length);
} catch (Exception e) {
e.printStackTrace();
}
return super.findClass(name);
}
public static void main(String[] args) {
CustomClassLoader customClassLoader = new CustomClassLoader();
try {
customClassLoader.loadClass("com.me.zookeeper.test.CustomClassLoader");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
當運行代碼customClassLoader.loadClass("com.me.zookeeper.test.CustomClassLoader")時,沒有呼叫自定義的findClass()方法
uj5u.com熱心網友回復:
類加載器加載本類?uj5u.com熱心網友回復:
https://blog.csdn.net/weixin_30342639/article/details/105389653,類加載器的本質是jvm初始化時,加載類的參考,方法到堆疊記憶體,堆記憶體,和堆疊方法區里,比如說你自己寫了一個xxxUtil類,你不希望AppClassLoader加載它,而是通過自定義加載器加載xxxUtil類,那就需要自己定義類加載器。------------------------------------------
而你上述的寫法是自己加載自己,那肯定是通過父類加載器加載了,而且還是在同一個類中運行,原始碼CustomClassLoader.java都在,不會進入自定義的加載器的,customClassLoader.loadClass("com.me.zookeeper.test.CustomClassLoader");這個loadClass是加載別人的
uj5u.com熱心網友回復:
我換了一個類好像還是不行
Class<?> aClass = customClassLoader.loadClass("com.me.zookeeper.test.Test");
uj5u.com熱心網友回復:
專案打完包之后,你要把Test.java這個檔案從硬碟里對應目錄com/me/zookeeper/test下移走uj5u.com熱心網友回復:
我換了一種方式,好像還是不行
public class CustomClassLoader extends ClassLoader {
public void test(){
System.out.println("測驗加載是否完成");
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
System.out.println(getSystemClassLoader());
String path = this.getClass().getResource("").getPath();
File file = new File(name);
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
int b = 0;
while ((b = fileInputStream.read()) >-1) {
byteArrayOutputStream.write(b);
}
byteArrayOutputStream.close();
fileInputStream.close();
byte[] bytes = byteArrayOutputStream.toByteArray();
String s = new String(bytes);
System.out.println(s);
return super.defineClass(name, bytes, 0, bytes.length);
} catch (Exception e) {
e.printStackTrace();
}
return super.findClass(name);
}
public static void main(String[] args) {
CustomClassLoader customClassLoader = new CustomClassLoader();
try {
Class<?> aClass = customClassLoader.loadClass("D:\\wok2\\zookeeper-test-main\\target\\classes\\com\\me\\zookeeper\\test\\Test.class");
System.out.println(aClass.getClassLoader());
System.out.println(aClass.getName());
Test customClassLoader1 = (Test) aClass.newInstance();
customClassLoader1.classLoaderRange();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
uj5u.com熱心網友回復:
這個應該不會吧,你打了斷點嗎轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283492.html
標籤:Java SE
上一篇:java repaint() 方法無法執行,不知道是哪個原因
下一篇:多執行緒