java自定义类加载器调用远程字节码,进入本地jvm运行

package com.load;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;

public class Client2 {

    public NetClassLoder classLoader;

    @Test
    public void test() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, InterruptedException {

        String domain = "http://localhost";
        URL clas = new File(domain).toURI().toURL();
        //jar 资源协议
       // String jarUrl="jar:"+domain+"!/";
        //远程jar文件
      //  URL jar = new URL(jarUrl);
        int i = 0;
        ConcurrentHashMap<String, ClassLoader> map = new ConcurrentHashMap<>();


            String name= "a"+i;
            i++;
        System.out.println("++++");
        //url类加载器
            classLoader = new NetClassLoder(domain);
        Class<?> aClass = classLoader.findClass("com.load.plugin.impl.DemoFunction");
        System.out.println(aClass);

        boolean assignableFrom = IFunction.class.isAssignableFrom(aClass);
        System.out.println(assignableFrom);
        IFunction o = (IFunction) aClass.newInstance();
        o.test();



        classLoader = new NetClassLoder(domain);
        Class<?> aClass3 = classLoader.findClass("com.load.plugin.impl.DemoFunction");
        System.out.println(aClass);

        boolean assignableFrom3 = IFunction.class.isAssignableFrom(aClass);
        System.out.println(assignableFrom3);
        IFunction o2 = (IFunction) aClass.newInstance();
        o2.test();
        System.gc();
        o.test();
        System.out.println(IFunction.class.getClassLoader());
        System.out.println(o2.getClass().getClassLoader());

        //jar资源文件的链接
       // JarURLConnection  jarConn = (JarURLConnection) jar.openConnection();

        //获取jar文件 下载下来 下载到临时文件中 window时temp中  获取到下载的jar 对象
     //   JarFile jarFile = jarConn.getJarFile();

       // initClassFile(jarFile);
    }
}

package com.load;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

public class NetClassLoder extends ClassLoader {

    private String url;

    public NetClassLoder(String url){
        this.url=url;
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("类加载器回收了");
    }

    @Override
    protected Class<?> findClass(String className) throws ClassNotFoundException {
        byte[] classByNet = findClassByNet(className);
        if(classByNet==null){
            throw new ClassNotFoundException();
        }
        return defineClass(className,classByNet,0,classByNet.length);
    }

    protected  byte[] findClassByNet(String className){
        // 从网络上读取的类的字节
        String path = pares2Url(className);
        try {
            URL url = new URL(path);
            InputStream ins = url.openStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int bufferSize = 4096;
            byte[] buffer = new byte[bufferSize];
            int bytesNumRead = 0;
            // 读取类文件的字节
            while ((bytesNumRead = ins.read(buffer)) != -1) {
                baos.write(buffer, 0, bytesNumRead);
            }
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String pares2Url(String className) {
        String replace = className.replace(".", "/");
        return url+"/"+replace+".class";
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值