There is no easy way to do this in Java, because Java is a compiled language, not a scripting language which is executed directly from the source code. However, Java 6 does have an API for calling the compiler, see the package javax.tools.
You want to dynamically execute the String
as a java command?
That is you want some way to say execute(h) and it should execute the SOP and print Hi?
I don't think that could be done. If I could execute arbitrary String as code during run time then the Java Bytecode verification would be moot - no point in doing byte code verification and allowing an arbitrary string to execute at runtime.
Why would you want to do something like that anyways?
While you could do it in Java 6 (and, I suppose, lower versions by writing to a file and compiling manually), you'd be better off using a scripting language. There are several ways to go about that, and it's included in Java 6.