繼承classloader并且重寫findclass方法就可以自定義一個類加載器,具體什么是類加載器以及類加載器的加載過程與順序下次再說,下面給出一個小demo
首先定義一個類,比如mytest,并且將其編譯成class文件,然后放到一個指定的文件夾下面,其中文件夾的最后幾層就是它的包名,這里我將這個編譯好的類放到 : /users/allen/desktop/cn/lijie/mytest.class
1
2
3
4
5
6
|
package cn.lijie; public class mytest { public void show() { system.out.println( "show test!" ); } } |
自定義的類加載器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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; } } |
測試的主函數:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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(); } } } |
執行主函數,調用外部class的show方法:
補充:java遠程加載class文件
1.在win上創建java文件并編譯
2.上傳到遠程服務器
3.編寫java代碼
準備:
引入jar包 ganymed-ssh2-262.jar
1.加載外部class要定義自己的類加載器
2.使用內存流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
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" ; //自己的遠程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)創建對象,默認22 conn = new connection(ip); //連接遠程服務 conn.connect(); //使用用戶名和密碼登錄 conn.authenticatewithpassword(user, password); this .conn = conn; return conn; } catch (ioexception e) { e.printstacktrace(); } return null ; } //返回遠程實例 public object loadeothclass(string url) throws exception{ if ( null ==conn) throw new exception( "請初始化鏈接" ); sftpv3client sc = new sftpv3client(conn); //創建ssh客戶端連接 inputstream is = new sftpinputstream(sc.openfilero(url)); //創建輸入流 byte [] b = this .readclassfile(is); class <?> c = super .defineclass(b, 0 , b.length); //定義class return c.newinstance(); //創建實例 } //讀取遠程class文件 private byte [] readclassfile(inputstream is){ byte [] b = new byte [ 1024 ]; int len; bytearrayoutputstream bos = null ; try { bos = new bytearrayoutputstream(); //內存流輸出 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; } } |
輸出結果:
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/qq_20641565/article/details/78744677