• 从文件中路径中加载一个class


    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class FileSystemClassLoader extends ClassLoader { 
    
        private String rootDir; 
    
        public FileSystemClassLoader(String rootDir) { 
            this.rootDir = rootDir; 
        } 
    
        protected Class<?> findClass(String name) throws ClassNotFoundException { 
            byte[] classData = getClassData(name); 
            if (classData == null) { 
                throw new ClassNotFoundException(); 
            } 
            else { 
                return defineClass(name, classData, 0, classData.length); 
            } 
        } 
    
        public byte[] getClassData(String className) { 
            String path = classNameToPath(className); 
            System.out.println(path);
            try { 
                InputStream ins = new FileInputStream(path); 
                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 (IOException e) { 
                e.printStackTrace(); 
            }
            return null; 
        } 
    
        private String classNameToPath(String className) { 
            return rootDir + File.separatorChar 
                    + className.replace('.', File.separatorChar) + ".class"; 
        } 
     }
  • 相关阅读:
    【转载】Chrome 0day漏洞:不要用Chrome查看pdf文件
    内网渗透闲谈
    Sticky Fingure安装教程
    局域网下交换机配置
    Access 2010 应用基础 单元三:SQL查询
    ESP8266 wifi干扰、钓鱼实现
    这是谁的锅
    svn/git的diff、patch
    sublime开发php必备工具集合(mac)
    gcc学习笔记
  • 原文地址:https://www.cnblogs.com/overstep/p/2920836.html
Copyright © 2020-2023  润新知