Compile Java source code from a string? [duplicate]

Sure. Have a look at the JavaCompiler class and the other classes in the javax.tools package.

They’ve been around since Java 1.6.

Here is some example code.

(As pointed out by @Sergey Tachenov in the comments, it needs JDK to be installed as the necessary tools.jar file comes with JDK but not JRE.)

Leave a Comment