如何执行控制台或GUI输入,就像是实际的Java代码? [英] How to execute console or GUI input as if it was actual Java code?

查看:375
本文介绍了如何执行控制台或GUI输入,就像是实际的Java代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够以相同的方式或尽可能接近地输入在运行期间(在执行持久性程序期间)执行的java命令/代码,如果它出现在程序的源代码(作为软件的一部分编程),使用GUI元素,如jTextArea。






下面的StackOverflow问题似乎是相关的,但是,他们的答案,似乎不是我在寻找。





STBC将在任何具有版本1.6的计算机上运行+ Java 插件 * JDK(AKA SDK)。



(*)STBC使用的API仅仅是编译器的公共接口在只有JDK分发的 tools.jar 中(尽管JDK的public JRE似乎也获得了 tools.jar )。这导致一些不寻常的要求,运行本机jar或Web启动应用程序。


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?)

解决方案

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:

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.

这篇关于如何执行控制台或GUI输入,就像是实际的Java代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆