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

前端 未结 2 1342
暗喜
暗喜 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:14

    Have you tried looking up existing threads for the same topic ; On-the-fly, in-memory java code compilation for Java 5 and Java 6

    for example.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题