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:
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
.
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
You can use Eclipse Scrapbook pages.
In Eclipse create a Scrapbook page. In your project, New->Other->Scrapbook page.
In the file, enter some text, select it and hit ctrl-U, and there you go.
To manage your imports, right click in the page and select Set Imports, where you can choose to import a package or a single class. This is persistent, and is saved with the page.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With