java自定義ClassLoader加載指定的class文件操作
繼承ClassLoader并且重寫findClass方法就可以自定義一個類加載器,具體什么是類加載器以及類加載器的加載過程與順序下次再說,下面給出一個小demo
首先定義一個類,比如MyTest,并且將其編譯成class文件,然后放到一個指定的文件夾下面,其中文件夾的最后幾層就是它的包名,這里我將這個編譯好的類放到 : /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; }}測試的主函數(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方法:
補充:java遠(yuǎn)程加載class文件
1.在win上創(chuàng)建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)建對象,默認(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)程實例 public Object loadeOthClass(String url) throws Exception{ if(null==conn) throw new Exception('請初始化鏈接'); 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)建實例 } //讀取遠(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é)果:
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. 在Android中使用WebSocket實現(xiàn)消息通信的方法詳解2. 淺談python出錯時traceback的解讀3. Python importlib動態(tài)導(dǎo)入模塊實現(xiàn)代碼4. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解5. windows服務(wù)器使用IIS時thinkphp搜索中文無效問題6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. Nginx+php配置文件及原理解析8. 利用promise及參數(shù)解構(gòu)封裝ajax請求的方法9. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法10. JSP數(shù)據(jù)交互實現(xiàn)過程解析
