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/

相关推荐
是馄饨呀1 分钟前
Apache Tomcat RewriteValve路径遍历漏洞(CVE-2025-55752)修复
java·tomcat·apache
木井巳3 分钟前
【网络原理】HTTP协议
java·网络·网络协议·http·fiddler
SimonKing3 分钟前
开源免费!传统项目也可以接入天爱验证码(TAC),坑我来填
java·后端·程序员
cui_ruicheng4 分钟前
C++关联容器进阶:unordered_map / set与详解
开发语言·c++
weipt6 分钟前
FBX转3DTiles带坐标的高效转换工具
python
sycmancia11 分钟前
C++——C++异常处理
开发语言·c++
xxxxxxllllllshi13 分钟前
java值传递和引用传递的区别?举例一些常见都笔试面试题说明,最后有速记口诀
java·开发语言
huabiangaozhi15 分钟前
Spring Cloud Gateway 整合Spring Security
java·后端·spring
HLC++16 分钟前
C++中的类和对象
开发语言·c++