Java 交互式解释器

发布于 2024-11-09 20:20:09 字数 122 浏览 2 评论 0原文

有没有一个好的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱她像谁 2024-11-16 20:20:09

既然 Scala 运行在 JVM 上,为什么不使用 Scala 解释器呢?
当我需要测试一些 Java 代码片段时,我就是这样做的。

能够使用制表符补全功能真是太好了。

scala> System.getPropert
getProperties   getProperty
scala> System.getProperty("user.home")
res0: String = c:\Users\robert

好的,当您执行以下操作时,您必须了解一些 Scala 语法:

scala> val testMap = new java.util.HashMap[String, java.util.List[String]]
testMap: java.util.HashMap[String,java.util.List[String]] = {}

scala> testMap.put("planets", java.util.Arrays.asList("Mars", "Jupiter", "Pluto"
))
res0: java.util.List[String] = null

scala> testMap.get("planets")
res1: java.util.List[String] = [Mars, Jupiter, Pluto]

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.

scala> System.getPropert
getProperties   getProperty
scala> System.getProperty("user.home")
res0: String = c:\Users\robert

Ok, you have to know a bit about Scala syntax when you do stuff like:

scala> val testMap = new java.util.HashMap[String, java.util.List[String]]
testMap: java.util.HashMap[String,java.util.List[String]] = {}

scala> testMap.put("planets", java.util.Arrays.asList("Mars", "Jupiter", "Pluto"
))
res0: java.util.List[String] = null

scala> testMap.get("planets")
res1: java.util.List[String] = [Mars, Jupiter, Pluto]
意中人 2024-11-16 20:20:09

过去我用的是beanshell。它真的很轻并且完成了工作。 http://www.beanshell.org/

In the past I used beanshell. It was really light and got the job done. http://www.beanshell.org/

月棠 2024-11-16 20:20:09

您可以使用 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 a private method, Dr. Java's interactions will let you use this method by instance.

乙白 2024-11-16 20:20:09

我在大学时使用过一点 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

往事随风而去 2024-11-16 20:20:09

从 Java 9 开始,JDK 附带了自己的 REPL,jshell
如果 java/bin 目录位于 PATH 上,您应该能够从命令行启动它。

$ jshell
|  Welcome to JShell -- Version 11.0.14
|  For an introduction type: /help intro

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

jshell> 1 + 1
$1 ==> 2

jshell> int foo(int x) {
   ...> return x + 1;
   ...> }
|  created method foo(int)

jshell> foo(5)
$3 ==> 6

您可以通过调用 /imports 来查看当前的导入。
Jshell 以默认导入开始,但您始终可以导入更多类,只要它们位于类路径上即可。

您可以启动 jshell 并提供类路径参数:

$ jshell --class-path /opt/libs/guava-19.0.jar:/opt/libs/commons-lang3-3.4.jar

请参阅此答案中有关类路径选项的更多信息。

Since Java 9 the JDK comes with it's own REPL, the jshell.
If the java/bin directory is on the PATH you should be able to start it from the command line.

$ jshell
|  Welcome to JShell -- Version 11.0.14
|  For an introduction type: /help intro

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

jshell> 1 + 1
$1 ==> 2

jshell> int foo(int x) {
   ...> return x + 1;
   ...> }
|  created method foo(int)

jshell> foo(5)
$3 ==> 6

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:

$ jshell --class-path /opt/libs/guava-19.0.jar:/opt/libs/commons-lang3-3.4.jar

See more regarding classpath options in this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文