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, a
Yes, there is: http://www.scravy.de/blog/2012-02-27/a-read-eval-print-loop-for-java.htm
There is simple IDE called DrJava that has an Interactions console. It works exactly as I would expect. Just load a file and start interacting with the objects in it.
BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript. You can use BeanShell interactively for Java experimentation and debugging as well as to extend your applications in new ways. Scripting Java lends itself to a wide variety of applications including rapid prototyping, user scripting extension, rules engines, configuration, testing, dynamic deployment, embedded systems, and even Java education.
http://www.beanshell.org/
http://www.beanshell.org/manual/syntax.html#Standard_Java_Syntax
Old question, but there is a better answer now (May 2013) - java-REPL! It's available on github and also available live at the java-repl website for quick one-off testing.
If you grab the git hub code and run ant
to generate the artifacts, you can make it easy to use with a small script like:
#!/bin/sh
java -jar /home/rdahlgren/scripts/javarepl-dev.build.jar
Since finding this project I probably use it 5 times a day. Enjoy!
Scala also offers an interactive console. I was able to use it to get a result for the expression in your question by fully qualifying InetAddress, as in:
java.net.InetAddress.getAllByName("localhost")
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.
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).
Then:
Now you have a scrapbook page. Type some code, like maybe this:
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.
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
.