8

Is it possible in Eclipse to have a Console, where when in Debug mode I can try out things? Like in python, where I can type at any moment for example 1+1 or myVariable.toString(), etc? C# has a similar concept with the F# shell.

I'd like to inspect my variables at run-time and try out some methods, having that kind of console would be of great help.

Thanks

devoured elysium
  • 101,373
  • 131
  • 340
  • 557

4 Answers4

13

Eclipse has the Display view, where you can enter (almost) arbitrary Java expressions and have them evaluated whenever you are in a breakpoint.

To have any expression evaluated select it (either in the Display view itself, or any open Java source file) and push one of the "inspect", "display" or "evaluate" buttons.

  • "inspect" executes the expression and shows the resulting value in a pop-up inspection view (similar to the Variables view).
  • "display" executes the expression and prints the resulting value into the Display view
  • "evaluate" executes the expression and does nothing else

There some very good help on debugging in Eclipse, specifically about inspecting variables and evaluating expressons.

If you want to test the behavior of some Java code while you're not debugging, then you might want to look into the Scrapbook feature.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • I've tried to write 1+1; or this.toString(); and nothing seems to happen. How does it work? – devoured elysium Mar 25 '10 at 16:09
  • 1
    @devoured: are you sure that you're currently debugging an application *and* are in a breakpoint? That's necessary because the execution needs some context (for example to know what `this` is). – Joachim Sauer Mar 25 '10 at 16:15
  • Yes, I am. The expressions view seems to work, but when I try to type things in the Display view it seems to do nothing. – devoured elysium Mar 25 '10 at 16:17
  • 1
    @devoured: and have you **selected** the expression you want to execute? – Joachim Sauer Mar 25 '10 at 16:19
  • Ah, yes you are right. I had to select it. But isn't there a way that allows me to just write things and press ENTER to execute them? Having to write them and then having to select them and click execute from the context menu seems odd. – devoured elysium Mar 25 '10 at 16:26
  • 1
    Then you're probably looking for a REPL. See this question: http://stackoverflow.com/questions/397488/is-there-something-like-pythons-interactive-repl-mode-but-for-java – Joachim Sauer Mar 25 '10 at 16:28
  • I couldn't find the Display view in v4.12.0, but there's a view called "Debug Shell" which seems the same. – Sam Jul 10 '19 at 10:23
2

Yes. Eclipse scrapbook - little known but very cool

File -> New -> Java -> Java Run/Debug -> Scrapbook

plodder
  • 2,304
  • 18
  • 19
1

Eclipse also has an old plug-in that seems to do exactly what you're looking for called Dr. Java.

David Fox
  • 10,603
  • 9
  • 50
  • 80
0

TL;DR; Yes.

In Java 9 there was a REPL added which allows exactly like you were doing in Python.

In Eclipse you can add QuickShell to use JShell inside the Eclipse IDE. The TM Terminal Eclipse plugin will also need to be installed.

Incidentally, in IntelliJ it's builtin via Tools | JShell Console.

boardtc
  • 1,304
  • 1
  • 15
  • 20