Create .java file and compile it to a .class file at runtime

前端 未结 2 1362
暗喜
暗喜 2021-01-16 05:37

I\'m generating a whole bunch of .java files from an XSD file using XJC. I also need to compile those files to .class files and use them at runtime via reflection.

T

2条回答
  •  独厮守ぢ
    2021-01-16 06:18

    So I finally figured this out...

    Apparently you could pass several files to the compiler at once, and this solves the symbol error. What a stupidly simple solution.

    public static void compile(String... files) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        compiler.run(null, null, null, files);
    }
    

提交回复
热议问题