Java 有交互式解释器吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5890657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:18:30  来源:igfitidea点击:

Is there an interactive interpreter for Java?

javacompilationinterpreter

提问by Mohamed Khamis

I want to execute java commands interactively from shell: is there a way to do so?

我想从 shell 以交互方式执行 java 命令:有没有办法这样做?

回答by McDowell

The closest thing I'm aware of is BeanShell.

我所知道的最接近的是BeanShell

回答by Paolo

Not an interactive interpreter or a shell, but consider Eclipse scrapbook pagesa possible option.

不是交互式解释器或外壳程序,但考虑将Eclipse 剪贴簿页面作为一个可能的选择。

The Java development toolkit (JDT) contributes a scrapbook facility that can be used to experiment and evaluate Java code snippets before building a complete Java program. Snippets are edited and evaluated in the Scrapbook page editor, with resultant problems reported in the editor.

From a Java scrapbook editor, you can select a code snippet, evaluate it, and display the result as a string. You can also show the object that results from evaluating a code snippet in the debuggers' Expressions View.

Java 开发工具包 (JDT) 提供了一个剪贴簿工具,可用于在构建完整的 Java 程序之前试验和评估 Java 代码片段。在剪贴簿页面编辑器中编辑和评估片段,并在编辑器中报告由此产生的问题。

从 Java 剪贴簿编辑器中,您可以选择代码片段,对其进行评估,并将结果显示为字符串。您还可以在调试器的表达式视图中显示通过评估代码片段而产生的对象。

Bonus: the scrapbook is an Eclipse default feature, so it's not required to install anything you don't already have.

奖励:剪贴簿是 Eclipse 的默认功能,因此不需要安装任何您还没有的东西。

回答by jajo87

I would recommend using DrJava http://drjava.org/. It could serve your purpose

我建议使用 DrJava http://drjava.org/。它可以达到你的目的

回答by Isaac Truett

No, it isn't possible (as far as I have found) to write and run arbitrary java snippets interactively from the command line.

不,不可能(据我所知)从命令行以交互方式编写和运行任意 Java 片段。

I was looking for something similar a few years ago. There's BeanShell, and JDistrowhich have some elements of a pure-Java shell. The closest I ever found was jshwhich was somebody's university project, as I recall, never met with any popularity, and was abandoned.

几年前我一直在寻找类似的东西。有BeanShellJDistro,它们具有纯 Java shell 的一些元素。我找到的最接近的是jsh,我记得它是某人的大学项目,从未受到任何欢迎,并被放弃了。

回答by ninjagecko

I think this person would like to be able to do something like the following hypothetical example java --cmd 'int x=5; System.out.print(x);'

我认为这个人希望能够做类似以下假设示例的事情 java --cmd 'int x=5; System.out.print(x);'

You can write your own program, let's call it java-snippet, which has a single command-line argument string called code. Insert the code snippet into the of temporary file.

您可以编写自己的程序,我们称之为java-snippet,它有一个名为 的命令行参数字符串code。将代码片段插入到临时文件中。

...main(...) {
    //vvv your program inserts code here vvv
    //INSERT_CODE_MARKER
    //^^^                                ^^^
}

Then your java-snippetprogram compiles the temporary file and immediately runs it.

然后你的java-snippet程序编译临时文件并立即运行它。

[edit: it seems the original poster did not in fact want this, but wants an interactive java interpreter-- leaving this answer here for posterity]

[编辑:似乎原始海报实际上并不想要这个,而是想要一个交互式 Java 解释器——把这个答案留在这里给后代]

回答by Shaunak

Check out this article. Does something similar I think. http://davidwinterfeldt.blogspot.com/2009/02/genearting-bytecode.html

看看这篇文章。做一些我认为类似的事情。 http://davidwinterfeldt.blogspot.com/2009/02/genearting-bytecode.html