How can I obtain an interactive shell (like Ruby's irb) for Java?
Asked Answered
M

5

17

I spent the last year doing Ruby development, and during that time I discovered irb, which makes running little "code experiments" easy and fun. I've recently switched teams and am now doing Java development, and I've found that I really miss my irb window. I would like to do the same thing for Java, but I don't know how.

I'm using Eclipse (groan), but Eclipse's bells and whistles are not the same thing.

Marinemarinelli answered 20/5, 2011 at 18:42 Comment(1)
I changed the wording of the question so as to stick with SO's "no tool recommendation" policy. FWIW, this question has been on SO for a long time (as of now: nearly 10 years), and looking at the stats, appears to have helped a number of people, including me. I'm not sure why it is now a problem. Nevertheless, I rephrased the question so that it continues to help people while not running afoul of the (unnecessarily restrictive) rules.Marinemarinelli
L
4

Would "Use an Eclipse Java Scrapbook page" be better ;-)

I haven't actually used irb, but if you just want to run code snippets inside of eclipse, then this is a simple way of doing it.

Lee answered 20/5, 2011 at 19:5 Comment(2)
+1. A long time ago, Eclipse used to be a Smalltalk IDE. Unfortunately, it lost most of the features that you would expect from a Smalltalk IDE, but some remnants still remain. The most obvious one is the incremental on-line background compiler which performs all kinds of stuff from syntax highlighting to code completion to type checking to refactoring, but the Scrapbook is another one. It is, of course, not nearly as good as a Smalltalk workspace, but we have come to expect that static language IDEs lag behind dynamic language IDEs by about 5-25 years.Strongbox
OK, I'll lift the Eclipse prohibition ;) This isn't quite what I had in mind, but it looks pretty neat. I would have never found that in the menus (nor have known it was what I was looking for).Marinemarinelli
D
22

BeanShell is probably what you're looking for.

https://github.com/beanshell/beanshell

Detain answered 20/5, 2011 at 18:45 Comment(1)
Yep, this is precisely what I was looking for. I found it after posting the question, of course. Thanks!Marinemarinelli
S
6

There are a few choices: BeanShell has been around for a long time, and is a great Java REPL.

Another alternative is groovy's groovysh or groovyConsole. While groovy isn't Java in the strict sense, it's great for interactively playing around with Java classes.

Selfconsistent answered 20/5, 2011 at 18:45 Comment(1)
+1. I often use Groovy exactly this way, as an environment for experimenting with Java classes and testing bits of code. Most Java code will run as-is, so Java programmers don't need to know Groovy to benefit from its shell. (It's not a bad idea to learn a bit of Groovy, though, since it simplifies many tasks and can improve your productivity while working with it.)Gambrill
R
5

Old question, I know, but jshell: The Java Shell (Read-Eval-Print Loop), is coming in Java 9 (Summer 2017, hopefully).

Ripen answered 23/2, 2017 at 16:28 Comment(0)
L
4

Would "Use an Eclipse Java Scrapbook page" be better ;-)

I haven't actually used irb, but if you just want to run code snippets inside of eclipse, then this is a simple way of doing it.

Lee answered 20/5, 2011 at 19:5 Comment(2)
+1. A long time ago, Eclipse used to be a Smalltalk IDE. Unfortunately, it lost most of the features that you would expect from a Smalltalk IDE, but some remnants still remain. The most obvious one is the incremental on-line background compiler which performs all kinds of stuff from syntax highlighting to code completion to type checking to refactoring, but the Scrapbook is another one. It is, of course, not nearly as good as a Smalltalk workspace, but we have come to expect that static language IDEs lag behind dynamic language IDEs by about 5-25 years.Strongbox
OK, I'll lift the Eclipse prohibition ;) This isn't quite what I had in mind, but it looks pretty neat. I would have never found that in the menus (nor have known it was what I was looking for).Marinemarinelli
S
0

Tried JShell and beanshell. Do suggest JShell over beanshell. Up key will roll back to history lines. Display much user friendly.

Thank you @Anders Sandvig

It can be found in jdk since java 9.

Salomo answered 21/11, 2019 at 0:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.