java编译多个文件,使用Java编译器API编译多个Java文件

Hi I have requirement to create ,compile and load java classes run time. Using FTL i am creating java source files , and able to compile the source if there is no dynamic dependency.

To elaborate with an instance, I have two java source file, one interface and its implementation class. I am able to compile the interface using java compiler api as follows

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

String testpath =classpath+";"+rootPath+"/lib/is_wls_client.jar;"+rootPath+"/rtds_wls_proxyclient.jar;.;";

File javaFile = new File(javaFileName+".java");

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

List optionList = new ArrayList();

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

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

Iterable fileObjects = sjfm.getJavaFileObjects(javaFile);

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

task.call();

sjfm.close();

I set class path for static classes which are already in the classpath , but this approach do not work for dynamically created classes? Any custom class loader will do the fix? My final implementation will be in web/app server

Any feedback will be highly appreciated

Satheesh

解决方案

I was able to solve this issue by compiling all the java files together. Using FTL I generate the java classes, and then compile it using java compiler api and load classes with custom class loader

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();

}

Below code snippet shows how to use custom class loader

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;

}

}

thanks

Satheesh

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值