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

发布于 2025-01-02 04:18:46 字数 1034 浏览 1 评论 0原文

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

为了详细说明一个实例,我有两个java源文件,一个接口及其实现类。我可以使用 java 编译器 api 编译接口,如下所示

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

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

任何反馈都将受到高度赞赏

Satheesh

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2025-01-09 04:18:46

我通过将所有 java 文件编译在一起解决了这个问题。使用FTL生成java类,然后使用java编译器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;
    }
}

谢谢
萨提什

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

    }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文