gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:02:42 27 4
gpt4 key购买 nike

嗨,我需要创建、编译和加载 java 类运行时。使用 FTL 我正在创建 java 源文件,并且如果没有动态依赖项,则能够编译源代码。

用一个实例来详细说明,我有两个java源文件,一个接口(interface)及其实现类。我能够使用java编译器api编译接口(interface)如下

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<String> optionList = new ArrayList<String>();
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();

我为已经在 classpath 中的静态类设置了类路径,但是这种方法不适用于动态创建的类?任何自定义类加载器都可以解决这个问题吗?我的最终实现将在 web/app 服务器中

任何反馈将不胜感激

萨西什

最佳答案

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

Java 编译器

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<String> optionList = new ArrayList<String>();
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;
}
}

谢谢
萨西什

关于java-compiler-api - 使用 Java Compiler API 编译多个 java 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9112640/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com