Is there something like python's interactive REPL mode, but for Java?

edit Since Java 9 there's 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.

It looks like this:

Screenshot of Groovy


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:

  1. In the project navigator window, select a folder that exists somewhere in your project.
  2. Either select the menu File -> New -> Other, or hit Control-N.
  3. Select Java -> Java Run/Debug -> Scrapbook Page.
  4. Hit "Next", then give it a filename, then hit "Finish".

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.


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