How to execute console or GUI input as if it was a

2019-02-25 01:18发布

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 code would be executed if it was present on the source-code for the program (programmed as part of the software), using a GUI element, like jTextArea.


The following StackOverflow questions seem to be related, but, along with they'r answers, don't seem to be what i'm looking for.

How To Get Input From Console Class In Java?

Using GUI for console input and outputin java

I don't want to "be able to trigger actions from specific console inputs, and print the feedback into the console", as seems to be the case of console applications described in those question and answers.

What i want is to "be able to execute actual java code, that i cannot predict nor control, from console (or GUI) input".

NOTE: The "i cannot predict nor control" here refers to the "developer"...But of course i can predict and "control" it if i'm the one inputting the code, as the "user" would do.


Since java uses a virtual-machine environment, i think it might be possible for it to execute "run-time inputted code"...But i'm not sure such thing is even possible.

For example, i'd like the run-time input of for(int i=0; i<3; i++){System.out.println(i);} in the "GUI console" (jTextArea, for example) to, upon pressing the enter key or clicking a "Send" button, be ("compiled and "?) executed, and give the same output as if it was part of the original source-code, thus:

0
1
2

So i'd like to know the following:

  • Is it possible?
  • If yes, how can i do it? (OR, if no, what is the closest alternative?)

4条回答
乱世女痞
2楼-- · 2019-02-25 01:36

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?

查看更多
Fickle 薄情
3楼-- · 2019-02-25 01:43

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

查看更多
劳资没心,怎么记你
4楼-- · 2019-02-25 01:54

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.

查看更多
三岁会撩人
5楼-- · 2019-02-25 01:57

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.

查看更多
登录 后发表回答