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;
  }
}

測試的主函數:

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.使用內存流

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; 
 } 
}

輸出結果:

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: