Jatin
Jatin

Reputation: 31724

Why no Repl mode for Java

Well I know it is a stupid question, but lets think on it for a second.

Why can't we have a repl mode or kind of a shell for Java. Say I just want to do Math.max(2,3) to get output 3. It can be similar to Scala repl mode, where by default the class and main declarations are handled (it extends to App) and only an expression is evaluated.

Technically:

  1. There is an interpreter running behind can execute it - If scala and groovy can, then that means the JVM is not the major issue
  2. If javac is the reason, say we remove all optimizations and it directly converts line by line strictly to java byte code, then there should be a way out. Type Inference might not be strong to show a lot of information, but atleast the output of methods can be shown. Probably javac can be a bit modified for such cases
  3. Ex: In the debugger, we attach break points to the line and then execute forward. A similar strategy can be applied

It would be cool to have a default shell for each JVM process, where on the run, one can access or set some say static variables and have live information.

All this would have been thought off, but why hasn't it been accepted by majority (there are some open source implementations though)

Upvotes: 3

Views: 637

Answers (4)

Albert Latacz
Albert Latacz

Reputation: 33

You could try http://www.javarepl.com/ or console version from https://github.com/albertlatacz/java-repl

Upvotes: 1

Deepak Bala
Deepak Bala

Reputation: 11185

Like @Sean suggested you can indeed use bean shell in repl mode.

java -cp ./bsh-2.0b4.jar bsh.Interpreter
BeanShell 2.0b4 - by Pat Niemeyer ([email protected])
bsh % System.out.println("Hello World");
Hello World

Alternatively, the less known Display tab on the eclipse IDE can execute arbitrary java code and evaluate expressions on the fly. Ctrl+U runs the expression and Ctrl+Shift+I evaluates them. It is not repl mode, but comes close.

Upvotes: 2

jc1001
jc1001

Reputation: 394

The Display view in Eclipse provides a lot of convenient behaviour in this area, with command completion, access to the running application stack, and console output through System.out.

Upvotes: 2

Sean Owen
Sean Owen

Reputation: 66886

This is more or less exactly what BeanShell does.

Upvotes: 4

Related Questions