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.
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.
A library like Javassist allows you to construct classes at runtime without having to create and compile files. You'd need a class each time you want to execute a piece of code, though.
a scripting language is usually something that runs programs on an OS, or another application. For example, you can write a unix script that goes something like
cd q $HOME\files
foreach $file ( @ files ) {
print "copying $file";
system ("rcp $file $dest") == 0 or die "it didn't work";
}
this script, when run, calls other programs used by the OS. you could easily enter these commands directly on the Unix prompt, and they'd run (except for the variables...).
Perl is another good example. It is trivial in Perl to wire together a bunch of OS commands.
According to the wikipeia, Firefox is written in C++, but can be controlled by the javascript scripting language.
Never ascribe to malice that which can be adequately explained by stupidity.