javacompiler编译java文件_java-compiler-api – 使用Java Compiler API编译多个java文件

我能够通过编译所有java文件来解决这个问题.使用FTL我生成java类,然后使用java编译器api编译它,并使用自定义类加载器加载类

Java Complier

private void compile(File[] files) throws IOException{

String classpath=System.getProperty("java.class.path");

String rootPath=getServletContext().getRealPath("/");

System.out.println("--> root Path "+rootPath);

String testpath=classpath+";.;xx.jar;yy.jar";

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

List optionList = new ArrayList();

optionList.addAll(Arrays.asList("-classpath",testpath));

// optionList.addAll(Arrays.asList("-d",rootPath+"/target"));

StandardJavaFileManager sjfm = compiler.getStandardFileManager(null, null, null);

Iterable fileObjects = sjfm.getJavaFileObjects(files);

JavaCompiler.CompilationTask task = compiler.getTask(null, null, null,optionList,null,fileObjects);

task.call();

sjfm.close();

}

下面的代码片段显示了如何使用自定义类加载器

class CustomClassLoader extends ClassLoader {

public CustomClassLoader(ClassLoader parent) {

super(parent);

}

public Class findClass(String className,String path) {

byte[] classData = null;

try {

FileInputStream f = new FileInputStream(path);

int num = f.available();

classData = new byte[num];

f.read(classData);

} catch (IOException e) {

System.out.println(e);

}

Class x = defineClass(className, classData, 0, classData.length);

return x;

}

}

谢谢Satheesh

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值