有没有类似 Python 的 Java 交互控制台?

最近我花了很多时间在 Java 编程上,脚本语言让我怀念的一件事就是在控制台上测试它们的能力。

为了快速测试一个 java 程序,我必须编辑一个文件,然后将其转换为字节码并执行它。即使使用 IDE,在第372次之后,它也失去了乐趣。

我想知道,是否有一个产品的功能类似于一个交互式控制台(我打赌你需要一个 JIT 编译器)和一些自动完成(与放松性,我认为这是可能的)。

也许这是一件非常普通的事情,我只是不知道或一些完全不可能的事情,但它最坏的问题: -)

44242 次浏览

Yes; jshell, and before that some close approximations are Groovy, Clojure, Scala, and the Bean Shell.

You may be interested in Groovy.

I've occasionally run into the same problem and have a partial solution. I keep around a file (as Charlie Martin said, Java needs its class contexts) that is little more than a test program. In a second window - a console - I have a script that I run that just checks the modification time of the source file every second or two. When it sees the source change, it re-compiles it (I'm usually fiddling in C, but I've done this with Java, as well) and executes the result.

It's not a great solution, but its a fast one-off and I've found it to be very useful.

An approach that I have used to some degree of success is to debug in Eclipse and use the display view. What the display view gives you is the ability to manipulate the code currently running in the JVM through executing Java statements. Any object available at the particular break point you are stopped at is in scope within the display view. While this isn't exactly what you are looking for it does provide some of the features that a REPL provides for other programming environments. See Debugging with the Eclipse Platform for more information.

Funnily enough, you get an interactive console with Jython ! You don't get much more Python-like.

You can execute Scala interactive interpreter too.

Jgrasp IDE have a interactive console where you can test in, i use it a lot, and there is alos a debug view showing variabel etc efter you have deklare them.

It is opensoruce and you can get a copy att http://www.jgrasp.org/

// Anders

You can use BeanShell to run arbitrary Java code. If you want Eclipse integration, EclipseShell has BeanShell support.

Try Dr Java's "Interaction Pane".

JShell is now standard in Java 9. JShell introduction