动态编辑加载自定义类

package test;

import java.io.File; 
import java.lang.reflect.Method; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.util.Arrays; 
 
import javax.tools.JavaCompiler; 
import javax.tools.JavaFileObject; 
import javax.tools.SimpleJavaFileObject; 
import javax.tools.StandardJavaFileManager; 
import javax.tools.ToolProvider; 
import javax.tools.JavaCompiler.CompilationTask; 
import javax.tools.JavaFileObject.Kind; 
 
public class JavaToolsTest { 
    public static void executeMain(String name, String content){ 
        Class<?> claszz = compile(name,content); 
        try { 
            Method method = claszz.getMethod("main", String[].class); 
            System.out.println(method.getName()); 
            method.invoke(null, new Object[] { new String[] {} }); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    private final static Class<?> compile(String name, String content) { 
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); 
        StrSrcJavaObject srcObject = new StrSrcJavaObject(name, content); 
        Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(srcObject); 
        String flag = "-d"; 
        String outDir = ""; 
        try { 
            File classPath = new File(Thread.currentThread().getContextClassLoader().getResource("").toURI()); 
            outDir = classPath.getAbsolutePath() + File.separator; 
        } catch (URISyntaxException e1) { 
            e1.printStackTrace(); 
        } 
        Iterable<String> options = Arrays.asList(flag, outDir); 
        CompilationTask task = compiler.getTask(null, fileManager, null, options, null, fileObjects); 
        boolean result = task.call(); 
        if (result == true) { 
            System.out.println("Compile it successfully."); 
            try { 
                return Class.forName(name); 
            } catch (ClassNotFoundException e) { 
                e.printStackTrace(); 
            } 
        } 
        return null; 
    } 
    private static class StrSrcJavaObject extends SimpleJavaFileObject { 
        private String content; 
        public StrSrcJavaObject(String name, String content) { 
            super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE); 
            this.content = content; 
        } 
        public CharSequence getCharContent(boolean ignoreEncodingErrors) { 
            return content; 
        } 
    } 
    public static void main(String[] args) throws Exception{ 
        StringBuffer sb = new StringBuffer(); 
        sb.append("package test;"); 
        sb.append("public class Test2{"); 
        sb.append("  public static void main(String[] args){"); 
        sb.append("    System.out.println(\"ok.\");"); 
        sb.append("  }"); 
        sb.append("}"); 
         
        executeMain("test.Test2", sb.toString()); 
    } 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值