zoukankan      html  css  js  c++  java
  • Java 加载指定位置java文件

    思路

    • 读取Person.java文件
    • 编译成.class文件
    • 加载.class文件,需要自定义类加载器
    • 获取class对象,通过反射API调用Person方法

    相关代码

    // 根据文件路径,编译并加载
    public static Class<?> compileAndLoad(String path) {
            String type = ".class";
            JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
            int result = javaCompiler.run(null, null, null, path);
            if (result != 0) {
                throw new RuntimeException("编译失败");
            }
            Class resultClass = null;
    
            String[] split = path.split("\.");
            String classLocation = split[0]+ type;
    
            // 自定义类加载器
            MyClassLoader myClassLoader = new MyClassLoader();
            try {
                resultClass = myClassLoader.findClass(classLocation);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return resultClass;
    }
    
         /**
         * 自定义类加载器
         */
        static class MyClassLoader extends ClassLoader{
    
            @Override
            protected Class<?> findClass(String name) throws ClassNotFoundException {
                Path path = null;
                byte[] bytes = null;
                try {
                    path = Paths.get(name);
                    bytes = Files.readAllBytes(path);
                } catch ( IOException e) {
                    e.printStackTrace();
                }
                String[] split = name.split("/");
                String fileName = split[split.length - 1];
                String className = fileName.split("\.")[0];
                // 调用父类
                return super.defineClass(className, bytes, 0, bytes.length);
            }
        }
    
    // 测试
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException {
            String path = "/Users/mars/codeSpace/0bat/3source_code/1jdk/upload/Person.java";
            Class<?> aClass = compileAndLoad(path);
            System.out.println(aClass.getClassLoader());
            Object o = aClass.newInstance();
            Method say = aClass.getDeclaredMethod("say",String.class);
            Object invoke = say.invoke(o, "wang");
            System.out.println(invoke.toString());
    
        }
    
    // 打印结果
    //reflect.demo2.LoadOutResource$MyClassLoader@457e2f02
    //hello,wang
    
    I'll put a flower in your hair~
  • 相关阅读:
    vue开发环境搭建
    超出宽度省略号,css解决办法
    css平板手机pc媒体查询
    pc和移动端调用qq聊天
    fiddeler 抓包工具的配置使用
    给证件照换背景颜色
    markman高效的设计稿标注、测量工具
    画三角形和直角三角形
    vue中使用Ueditor编辑器 -- 1
    js获取浏览器缩放比例
  • 原文地址:https://www.cnblogs.com/Yuanye-Wong/p/14513958.html
Copyright © 2011-2022 走看看