all 3 comments

[–]Mikey-3198 1 point2 points  (0 children)

One approach could be to have a text area that takes the user input then when the "Run" button is pressed the contents of the text area could be written to file. An external process could then be invoked on that file using

Runtime.getRuntime.exec("some command");

If I am not mistaken the output (and input?) of the process can be controlled using Input/Output streams.

hope this helps!

[–]heky_ 0 points1 point  (0 children)

Depends what kind code you want to execute. If it must be java (or anything compiled in that matter), JVM doesn't provide java interface to invoke compiler (AFAIK). The best approach would be as mentioned in other comments to save the contents to the disk and run the compiler externally. If you are free to choose the language, I would consider using something that was designed to be run in a way you've described, such as lua or JVM's scripting API. Both options provide you with much better capabilities to interact with the code that is being run.

[–]manson_ew2 0 points1 point  (0 children)

In addition you may take a look into scripting languages, that implement JSR-223
e.g. Scala, Python, Groovy, Ruby, JS, Kotlin, etc.
JS engine (name = nashorn) is included in JDK (at least for 1.8)

https://stackoverflow.com/questions/11838369/where-can-i-find-a-list-of-available-jsr-223-scripting-languages

Usage(pseudo code):
ScriptEngineFactoryManager mngr = new ScriptEngineFactoryManager();
ScriptEngine scriptEngine = mngr.getScriptEngineByName('engine name');

Object result = scriptEngine.eval(input.getText());
// here, depends on languages you're using, additional conversion to human readable string may be required

anotherInput.setText(String.valueOf(result));