How do I programmatically compile and instantiate a Java class?

前端 未结 3 1305
夕颜
夕颜 2020-11-22 03:04

I have the class name stored in a property file. I know that the classes store will implement IDynamicLoad. How do I instantiate the class dynamically?

Right now I h

3条回答
  •  死守一世寂寞
    2020-11-22 03:15

    In the same vein as BalusC's answer, but a bit more automatic wrapper is here in this piece of code from my kilim distribution. https://github.com/kilim/kilim/blob/master/src/kilim/tools/Javac.java

    It takes a list of strings containing Java source, extracts the package and public class/interface names and creates the corresponding directory/file hierarchy in a tmp directory. It then runs the java compiler on it, and returns a list of name,classfile pairs (the ClassInfo structure).

    Help yourself to the code. It is MIT licensed.

提交回复
热议问题