Java 交互式解释器
有没有一个好的 Java 交互式解释器,类似于 Scala 的?编程时,我喜欢尝试一小段代码,看看它们是否像我预期的那样工作,然后再将它们插入到我的主程序中。我更喜欢不在线的口译员,因为我经常离线工作。
提前致谢!
Is there a good interactive interpreter for Java, similar to Scala's? When programming, I like to try small pieces of code to see if they work like I expect before plugging them in to my main program. I would prefer an interpreter that is not based online since I often work offline.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
既然 Scala 运行在 JVM 上,为什么不使用 Scala 解释器呢?
当我需要测试一些 Java 代码片段时,我就是这样做的。
能够使用制表符补全功能真是太好了。
好的,当您执行以下操作时,您必须了解一些 Scala 语法:
Since Scala runs on a JVM, why not the Scala interpreter?
That's what I do when I need to test a few Java-snippets.
It's nice to have tab completion that works.
Ok, you have to know a bit about Scala syntax when you do stuff like:
过去我用的是beanshell。它真的很轻并且完成了工作。 http://www.beanshell.org/
In the past I used beanshell. It was really light and got the job done. http://www.beanshell.org/
您可以使用 Dr. Java。它有一个类似于解释器的交互窗口。我不知道他们是如何开发它的,所以你最好谨慎使用它。例如,如果您有一个带有
private
方法的类,Dr. Java
的交互
将允许您通过实例使用此方法。You can use Dr. Java. It has an
interactions
window that acts like an interpreter. I don't know how they developed it though, so, you better use it cautiously. E.g. if you have a class with aprivate
method,Dr. Java
'sinteractions
will let you use this method by instance.我在大学时使用过一点 BlueJ。它最初是作为一种教学工具,非常适合修改代码,并且该项目得到(曾经?)Sun 的支持,所以我想这也是它的优点。我已经有一段时间没有使用它了,但我记得它的好处是你可以修改你编写的代码而无需编写 main() 类。你可以编写一个类、创建一个实例、调用方法(它会提示你输入参数)等等。它还有一个视觉方面,有点类似于 Eclipse 中的检查器。
http://www.bluej.org/about/what.html
I used BlueJ a bit in college. It was started as a teaching tool, it's perfect for tinkering with code and the project is (was?) supported by Sun so I guess that's a feather in its cap too. It's been a while since I've used it but the nice thing I remember about it is that you can tinker with the code you write without writing a main() class. You can write a class, create an instance, call methods (it will prompt you for parameters) and so on. There's also a visual aspect to it as well, somewhat comparable to the inspector in Eclipse.
http://www.bluej.org/about/what.html
从 Java 9 开始,JDK 附带了自己的 REPL,jshell。
如果
java/bin
目录位于PATH
上,您应该能够从命令行启动它。您可以通过调用
/imports
来查看当前的导入。Jshell 以默认导入开始,但您始终可以导入更多类,只要它们位于类路径上即可。
您可以启动 jshell 并提供类路径参数:
请参阅此答案中有关类路径选项的更多信息。
Since Java 9 the JDK comes with it's own REPL, the jshell.
If the
java/bin
directory is on thePATH
you should be able to start it from the command line.You can see the current imports by calling
/imports
.Jshell starts with default imports but you can always import more classes as long as they are on the classpath.
You can start the jshell and provide a classpath argument:
See more regarding classpath options in this answer.