87

I spent a lot of time programming in Java recently, and one thing I miss from scripting languages was the ability to test them in a console.

To quickly test a java program, I have to edit a file, then turn it to bytecode and execute it. Even using an IDE, it loses its fun after the 372 th time.

I would like to know if there is a product out there that features anything like an interactive console (I bet you need a JIT compiler) and some autocompletion (with relexivity, I suppose it's possible).

Maybe that's something very common that I just don't know about or something completely impossible, but its worst asking :-)

Bite code
  • 578,959
  • 113
  • 301
  • 329

11 Answers11

66

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

user1133275
  • 2,642
  • 27
  • 31
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • 1
    Agreed. I use BeanShell through [JDE](http://jdee.sourceforge.net/) in emacs; it's far from perfect, but it provides an interactive console, and decent completion support, both in the console and while editing your regular Java files. – Julian Squires Apr 24 '09 at 20:25
  • Indeed, it's very primitive but much better than nothing. Thank to you, and thanks to the guys who coded it :-) – Bite code Apr 24 '09 at 22:08
  • 3
    Also Scala with Eclipse IDE plugin, has interactive console for REPL style execution. See http://www.scala-ide.org/ – Hendy Irawan Apr 12 '11 at 17:53
  • @JulianSquires: is there a step-by-step anywhere? – serv-inc Feb 18 '18 at 15:16
  • I haven't used that BeanShell setup in many years. I would start from https://www.emacswiki.org/emacs/JavaDevelopmentEnvironment if I were trying to set it up again. – Julian Squires Feb 19 '18 at 16:11
  • It's amazing how much things change in 9 years. – Charlie Martin Mar 15 '18 at 18:47
13

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

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
12

Java REPL http://www.javarepl.com/term.html

with an intellij plugin: http://plugins.jetbrains.com/plugin/7215?pr=

Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
David Portabella
  • 12,390
  • 27
  • 101
  • 182
9

Try Dr Java's "Interaction Pane".

Harry
  • 91
  • 1
  • 1
  • Yeah I really miss it. now I change to Eclipse but I don't know how to get an Interaction Pane in Eclipse. – Tony Chen Dec 24 '16 at 05:40
9

JShell is now standard in Java 9. JShell introduction

user2144406
  • 300
  • 6
  • 10
5

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.

laz
  • 28,320
  • 5
  • 53
  • 50
  • I still prefer Scala IDE Eclipse plugin, however when it's not desirable to install a plugin, the Debug approach is the closest you can have to a REPL interpreter, that's built-in "feature" in Eclipse JDT. :) – Hendy Irawan Apr 12 '11 at 18:03
  • How to display the value of an expression from th display view? For instance, if I write something like: String s = "Hello World", s1 = s.substring ( 3, 5 ); how to know the value of s1? I cannot find a way – zakmck Mar 01 '12 at 18:53
  • You can highlight `s1` then bring up the context menu. There is an entry on there labeled `Display` that will do what you want. – laz Mar 02 '12 at 03:20
4

You can execute Scala interactive interpreter too.

stepancheg
  • 4,262
  • 2
  • 33
  • 38
2

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

neu242
  • 15,796
  • 20
  • 79
  • 114
2

You may be interested in Groovy.

Patrick McDaniel
  • 1,073
  • 4
  • 11
  • 28
1

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

Anders
  • 11
  • 1
0

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.

Sniggerfardimungus
  • 11,583
  • 10
  • 52
  • 97