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

package cn.lijie;public class MyTest { public void show() { System.out.println('show test!'); }}自定義的類(lèi)加載器:
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文件并編譯

準(zhǔn)備:
引入jar包 ganymed-ssh2-262.jar
1.加載外部class要定義自己的類(lèi)加載器
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)); }}//自定義類(lèi)加載器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(); //使用用戶(hù)名和密碼登錄 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客戶(hù)端連接 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è)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. CentOS郵箱服務(wù)器搭建系列——SMTP服務(wù)器的構(gòu)建( Postfix )2. asp文件用什么軟件編輯3. PHP基礎(chǔ)之生成器4——比較生成器和迭代器對(duì)象4. ASP新手必備的基礎(chǔ)知識(shí)5. Docker 啟動(dòng)Redis 并設(shè)置密碼的操作6. vue+element開(kāi)發(fā)一個(gè)谷歌插件的全過(guò)程7. Vue axios獲取token臨時(shí)令牌封裝案例8. JS中6個(gè)對(duì)象數(shù)組去重的方法9. 利用CSS制作3D動(dòng)畫(huà)10. Spring如何替換掉默認(rèn)common-logging.jar

網(wǎng)公網(wǎng)安備