有没有类似于 python 的交互式 REPL 模式的东西,但对于 Java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/397488/
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
Is there something like python's interactive REPL mode, but for Java?
提问by Yoni Roit
Is there something like python's interactive REPL mode, but for Java?
So that I can, for example, type InetAddress.getAllByName( localHostName )
in a window, and immediately get results, without all this public static void nightmare() thing?
有没有类似于 python 的交互式 REPL 模式的东西,但对于 Java?这样我就可以,例如,InetAddress.getAllByName( localHostName )
在一个窗口中输入,并立即获得结果,而没有所有这些 public static void nightmare() 的事情?
采纳答案by OscarRyz
editSince Java 9 there's JShell
编辑自 Java 9 有JShell
Original answer follows
原答案如下
You can also use Groovy Console. It is an interactive console where you can do what you want. Since Groovy also includes classes from the core java platform, you'll be able to use those classes as well.
您还可以使用Groovy 控制台。它是一个交互式控制台,您可以在其中做您想做的事。由于 Groovy 还包含来自核心 Java 平台的类,因此您也可以使用这些类。
It looks like this:
它看起来像这样:
回答by krosenvold
Most IDE's have a window called something like "immediate mode" that will allow you to evaluate java code on the fly.
大多数 IDE 都有一个类似于“立即模式”的窗口,可让您即时评估 Java 代码。
回答by speciousfool
Jythonis a python implementation which lets you inspect and interact with Java objects.
Jython是一个 Python 实现,它允许您检查 Java 对象并与之交互。
>>> from java.net import *
>>> InetAddress.getAllByName("google.com")
array(java.net.InetAddress,[google.com/209.85.171.100,
google.com/74.125.45.100,
google.com/74.125.67.100])
回答by Ole
The groovy consoleallows you to do that. It actually was meant to try and test groovy code, but since groovy is a superset of Java, it allows plain Java stuff as well.
在常规控制台可以让你做到这一点。它实际上是为了尝试和测试 groovy 代码,但由于 groovy 是 Java 的超集,它也允许使用普通的 Java 代码。
I just entered this into the console:
我刚刚在控制台中输入了这个:
InetAddress.getAllByName('localhost')
and hit CTRL-R, then it returned:
并按 CTRL-R,然后它返回:
groovy> InetAddress.getAllByName('localhost')
Result: [localhost/127.0.0.1]
回答by Ande Turner
Clojure provides a REPL you can use.
Clojure 提供了一个您可以使用的 REPL。
回答by Tim Jansen
As an alternative to Groovy, try Beanshell: http://www.beanshell.org/
作为 Groovy 的替代方案,请尝试 Beanshell:http: //www.beanshell.org/
It is more Java-like and allows you to use Java-syntax directly.
它更像 Java,并允许您直接使用 Java 语法。
回答by joel.neely
回答by jfklein
For folks with access to Mathematica, JLink lets you access Java and script with Mathematica code:
对于可以访问 Mathematica 的人,JLink 允许您使用 Mathematica 代码访问 Java 和脚本:
Needs["JLink`"]
LoadJavaClass["java.net.InetAddress"]
InetAddress`getAllByName["localhost"]
Hit Shift-Enter to evaluate, and you get
按 Shift-Enter 进行评估,您将得到
{<<JavaObject[java.net.Inet4Address>>}
Then you can use Mathematica's Map function to call toString
on the returned objects:
然后你可以使用 Mathematica 的 Map 函数调用toString
返回的对象:
#@toString[]& /@ %
to get the result (or to use the less obscure syntax, Map[Function[obj, obj@toString[]], %]
):
得到结果(或使用不那么晦涩的语法,Map[Function[obj, obj@toString[]], %]
):
{"localhost/127.0.0.1"}
If you start to get serious with this, you'll want to read Todd Gayley's tutorial at http://reference.wolfram.com/mathematica/JLink/tutorial/Overview.html.
如果您开始认真对待这个问题,您将需要在http://reference.wolfram.com/mathematica/JLink/tutorial/Overview.html上阅读 Todd Gayley 的教程。
回答by jfklein
Eclipse has a feature to do this, although it's not a loop. It's called a "Scrapbook Page". I assume the analogy is supposed to be that you have a scrapbook where you collect little snippets of code.
Eclipse 具有执行此操作的功能,尽管它不是循环。它被称为“剪贴簿页面”。我假设类比应该是您有一个剪贴簿,您可以在其中收集小代码片段。
Anyway, to make it work, open a project in Eclipse (your Scrapbook Page is going to be associated with a project -- Eclipse likes it when projects own things).
无论如何,要使其工作,请在 Eclipse 中打开一个项目(您的剪贴簿页面将与一个项目相关联——Eclipse 喜欢项目拥有的东西)。
Then:
然后:
- In the project navigator window, select a folder that exists somewhere in your project.
- Either select the menu File -> New -> Other, or hit Control-N.
- Select Java -> Java Run/Debug -> Scrapbook Page.
- Hit "Next", then give it a filename, then hit "Finish".
- 在项目导航器窗口中,选择项目中某处存在的文件夹。
- 选择菜单 File -> New -> Other,或者点击 Control-N。
- 选择 Java -> Java 运行/调试 -> 剪贴簿页面。
- 点击“下一步”,然后给它一个文件名,然后点击“完成”。
Now you have a scrapbook page. Type some code, like maybe this:
现在你有一个剪贴簿页面。输入一些代码,就像这样:
System.out.println(System.getProperties());
System.out.println(System.getProperties());
Then select the text with the mouse, and either hit Control-U or select "Execute" from the context menu. The code will run and the output will appear on the console.
然后用鼠标选择文本,然后点击 Control-U 或从上下文菜单中选择“执行”。代码将运行,输出将显示在控制台上。
You can also type an expression, select it, and select Display from the context menu. It'll evaluate the expression and print its type. For example, running Display on 1 + 2
will print (int) 3
.
您还可以键入表达式,选择它,然后从上下文菜单中选择显示。它将评估表达式并打印其类型。例如,运行 Display on1 + 2
将打印(int) 3
。
回答by Trevor Tippins
You could take a look at BlueJwhich is an interactive Java development environment meant for teaching OOP rather than as full IDE like Eclipse or NetBeans. It's kind of fun to have a play with anyway.
您可以看看BlueJ,它是一个交互式 Java 开发环境,用于教授 OOP,而不是像 Eclipse 或 NetBeans 这样的完整 IDE。反正一起玩也挺好玩的。
You can see it in action on YouTubein a series of Java tutorials.
您可以在YouTube上的一系列 Java 教程中看到它的运行情况。