How to execute console or GUI input as if it was actual Java code?

前端 未结 4 934
眼角桃花
眼角桃花 2021-01-21 08:37

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

相关标签:
4条回答
  • 2021-01-21 08:56

    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.

    STBC GUI

    But note the:

    System Requirements

    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.

    0 讨论(0)
  • 2021-01-21 08:57

    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. "

    0 讨论(0)
  • 2021-01-21 09:14

    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?

    0 讨论(0)
  • 2021-01-21 09:21

    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.

    0 讨论(0)
提交回复
热议问题