You want the equivalent of JavaScript's eval. There is no equivalent in Java.
Well, there is but it's not trivial.
You can generate the full source code of a class containing that code. Something like
public class StuffToDynamicallyCompile  {
   public static final void main(String[] ignored)  {
       PUT STUFF HERE!
   }
}
And then programatically invoke the compiler, as described in this answer, or as stated in the comments: How to compile .java file from within java program
Not a simple task. Perhaps there's a way to minimize your requirements, so you can allow an extremely limited set of commands, and just execute it with a switch ("if 'dothis' then call doThis(), else if 'doThat', call doThat(), etc.).