java generate class file at runtime

前端 未结 1 880
甜味超标
甜味超标 2021-01-18 17:51

I need to generate class at runtime, each class is mapped to a database table. Such class is model class used in ORM.

When client specify a database table to work w

1条回答
  •  余生分开走
    2021-01-18 18:33

    Yes, this is possible. And you don't need an external lib. The JDK provides everything you need:

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    int compilationResult = compiler.run(null, null, null, fileToCompile);
    if (compilationResult == 0) {
        System.out.println("Compilation is successful");
    } else {
        System.out.println("Compilation Failed");
    }
    

    Check out the JavaCompiler docs.

    0 讨论(0)
提交回复
热议问题