Use JavaCompiler . It can compile code from String , so I'm sure that it can process code from a text file.
What do you think, instead of putting it in the main method, I can include it, for example, in a testing method and a calling method like this?
Put it anywhere. EG. see STBC and especially the source code . It provides a graphical interface and can compile code in the text area at the click of a button.

this program needs tools.jar, but jre 7 does not have this !!
Have you tried reading the documentation provided for STBC? In particular:
STBC will run on any computer with version 1.6+ Java Plug-In * JDK (AKA SDK).
(*) The API that STBC uses is just an open interface for the compiler in tools.jar , which is distributed only with the JDK (although the "public JRE" JDK tools.jar acquire tools.jar ). This leads to some unusual requirements when launching either your own banner or web launch application.
Or in short, no JRE will have a JavaCompiler , only they have a JDK.
source share