继承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;
}
}
输出结果:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。