【问题标题】:Using Java Compiler API to compile multiple java files使用 Java Compiler API 编译多个 java 文件
【发布时间】:2012-02-25 03:01:12
【问题描述】:

您好,我需要创建、编译和加载 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();

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

我们将不胜感激任何反馈

萨西

【问题讨论】:

    标签: java-compiler-api dynamic-class-loaders


    【解决方案1】:

    我能够通过将所有 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();
    
        }
    

    下面的代码 sn-p 展示了如何使用自定义类加载器

    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;
        }
    }
    

    谢谢 萨西斯

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多