I want to be able to input java commands/code to execute during run-time (during the execution of a \"persistent\" program) in the same way, or as close as possible, to the same
Use the JavaCompiler. It can compile code from a String
.
For an E.G. see the STBC & especially the source code. It provides a GUI and can compile the code in the text area on button click.
But note the:
STBC will run on any computer with a version 1.6+ Java Plug-In* JDK (AKA SDK).
(*) The API that STBC uses is merely a public interface to the compiler in the tools.jar
that is distributed only with JDKs (though the 'public JRE' of the JDK also seems to acquire a tools.jar
). This leads to some unusual requirements in running either the native jar, or the web start app.
There is a project called BeanShell
"In short, BeanShell is dynamically interpreted Java, plus a scripting language and flexible environment all rolled into one clean package. "
I know this is an old answer but for future Googlers:
I would recommend JavaREPL whose source is available here: https://github.com/albertlatacz/java-repl
What AlmightyR is asking for is called Read-Eval-Print-Loop (REPL) for the Java language which is what JavaREPL provides.
JavaREPL has an online demo available here: http://www.javarepl.com/console.html
JavaREPL also has an Intellij plugin and a CLI version which are both linked to in the Github repository.
It looks sort of abandoned currently but perhaps it just doesn't need to be maintained?
You can use JavaCompiler, as this question's answer states:
https://stackoverflow.com/a/935316/420001
Also, what you're wanting to do is evaluate
a String
of code
:
It's not really recommended though.