hutool java代码字符串动态加载类

java 复制代码
public class Test {
    public static void main(String[] args) throws Exception {
        ClassLoader compile = CompilerUtil.getCompiler(null)
                .addSource("Hello", "public class Hello{public static void main(String[] args){System.out.println(\"Hello World!\");}}")
                .compile();
        Class<?> hello = compile.loadClass("Hello");
        // 实例化对象c
//        Object obj = ReflectUtil.newInstance(hello);
        hello.getMethod("main", String[].class).invoke(null, (Object) new String[]{});
    }
}

https://doc.hutool.cn/pages/CompilerUtil/

相关推荐
吃饱了得干活14 小时前
Spring Cloud Gateway 微服务网关:路由、断言、过滤器
java·spring cloud
Warson_L14 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅15 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅15 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L15 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅15 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
lwx5728016 小时前
探秘InnoDB:搞懂它的内存、线程、磁盘与日志刷盘策略
java·后端
Warson_L16 小时前
python的类&继承
python
Warson_L16 小时前
类型标注/type annotation
python
Flynt17 小时前
从Spring Boot 4.0升到4.1,我在Maven和gRPC上栽了跟头
java·spring boot·后端