java自定義ClassLoader加載指定的class文件操作
繼承ClassLoader并且重寫findClass方法就可以自定義一個(gè)類加載器,具體什么是類加載器以及類加載器的加載過程與順序下次再說,下面給出一個(gè)小demo
首先定義一個(gè)類,比如MyTest,并且將其編譯成class文件,然后放到一個(gè)指定的文件夾下面,其中文件夾的最后幾層就是它的包名,這里我將這個(gè)編譯好的類放到 : /Users/allen/Desktop/cn/lijie/MyTest.class

package cn.lijie;
public class MyTest {
public void show() {
System.out.println("show test!");
}
}
自定義的類加載器:
public class MyClassLoader extends ClassLoader {
@Override
protected Class<?> findClass(String name) {
String myPath = "file:///Users/allen/Desktop/" + name.replace(".","/") + ".class";
System.out.println(myPath);
byte[] cLassBytes = null;
Path path = null;
try {
path = Paths.get(new URI(myPath));
cLassBytes = Files.readAllBytes(path);
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
Class clazz = defineClass(name, cLassBytes, 0, cLassBytes.length);
return clazz;
}
}
測(cè)試的主函數(shù):
public class MainClass {
public static void main(String[] args) throws ClassNotFoundException {
MyClassLoader loader = new MyClassLoader();
Class<?> aClass = loader.findClass("cn.lijie.MyTest");
try {
Object obj = aClass.newInstance();
Method method = aClass.getMethod("show");
method.invoke(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}
執(zhí)行主函數(shù),調(diào)用外部class的show方法:

補(bǔ)充:java遠(yuǎn)程加載class文件
1.在win上創(chuàng)建java文件并編譯

2.上傳到遠(yuǎn)程服務(wù)器

3.編寫java代碼
準(zhǔn)備:
引入jar包 ganymed-ssh2-262.jar
1.加載外部class要定義自己的類加載器
2.使用內(nèi)存流
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SFTPInputStream;
import ch.ethz.ssh2.SFTPv3Client;
public class Fs{
public static void main(String[] args) throws Exception {
OwnClassLoader ocl = new OwnClassLoader();
String ip,user,password;
ip = "120.34.168.80";//自己的遠(yuǎn)程ip
user = "root";//username
password = "123456";//password
ocl.login(ip, user, password);
Object obj = ocl.loadeOthClass("/opt/4/tt.class");//class文件路徑
System.out.println(obj);
Class c = obj.getClass();
Field f = c.getDeclaredField("age");
f.setAccessible(true);
System.out.println("age:"+f.get(obj));
}
}
//自定義類加載器
class OwnClassLoader extends ClassLoader{
private Connection conn = null;
//初始化鏈接
public Connection login(String ip,String user,String password){
Connection conn = null;
try {
//也可以new Connection(ip, port)創(chuàng)建對(duì)象,默認(rèn)22
conn = new Connection(ip);
//連接遠(yuǎn)程服務(wù)
conn.connect();
//使用用戶名和密碼登錄
conn.authenticateWithPassword(user, password);
this.conn = conn;
return conn;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//返回遠(yuǎn)程實(shí)例
public Object loadeOthClass(String url) throws Exception{
if(null==conn)
throw new Exception("請(qǐng)初始化鏈接");
SFTPv3Client sc = new SFTPv3Client(conn);//創(chuàng)建ssh客戶端連接
InputStream is = new SFTPInputStream(sc.openFileRO(url));//創(chuàng)建輸入流
byte[] b = this.readClassFile(is);
Class<?> c = super.defineClass(b, 0, b.length);//定義class
return c.newInstance();//創(chuàng)建實(shí)例
}
//讀取遠(yuǎn)程class文件
private byte[] readClassFile(InputStream is){
byte[] b = new byte[1024];
int len;
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();//內(nèi)存流輸出
while((len=is.read(b))!=-1){
bos.write(b, 0, len);
}
b = bos.toByteArray();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(is!=null)
is.close();
if(bos!=null)
bos.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
return b;
}
}
輸出結(jié)果:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Maven的porfile與SpringBoot的profile結(jié)合使用案例詳解
這篇文章主要介紹了Maven的porfile與SpringBoot的profile結(jié)合使用,通過maven的profile功能,在打包的時(shí)候,通過-P指定maven激活某個(gè)pofile,這個(gè)profile里面配置了一個(gè)參數(shù)activatedProperties,不同的profile里面的這個(gè)參數(shù)的值不同,需要的朋友可以參考下吧2021-12-12
IDEA源碼修改器JarEditor使用(反編譯-打包一步到位)
JarEditor是一個(gè)IDEA插件,用于修改jar包中的類文件,它允許用戶在不解壓jar包的情況下,直接在IDEA中編輯和修改類文件的源碼,修改完成后,可以一鍵編譯并生成新的jar包,替換原jar包2025-01-01
Spring+Mybatis 實(shí)現(xiàn)aop數(shù)據(jù)庫(kù)讀寫分離與多數(shù)據(jù)庫(kù)源配置操作
這篇文章主要介紹了Spring+Mybatis 實(shí)現(xiàn)aop數(shù)據(jù)庫(kù)讀寫分離與多數(shù)據(jù)庫(kù)源配置操作,需要的朋友可以參考下2017-09-09
eclipse實(shí)現(xiàn)ElGamal數(shù)字簽名
這篇文章主要為大家詳細(xì)介紹了eclipse實(shí)現(xiàn)ElGamal數(shù)字簽名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
生產(chǎn)環(huán)境NoHttpResponseException異常排查解決記錄分析
這篇文章主要為大家介紹了生產(chǎn)環(huán)境NoHttpResponseException異常排查解決記錄分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

