Java 9 REPL
With the recent release of Java comes the long awaited feature of REPL. I have been a fan of REPL esp with Scala.
So installed Java9 explored the options.
Following is the condensed version/notes of all my experimentation so far
Every execution in REPL is called snippet and you invoke it by calling jshell from $jdk9_home/bin
/list -all -start (stat snippets)
Other options are
/imports - lists all the imports so far
/vars - lists the variables
/methods - lists the methods
/type - lists the classes created
One good thing REPL supports both forward and backward referencing. i.e. You can create a method taking Foo object even before creation of Foo class
Some of the repetition snippet options are:
/! - return last snippet
/-{n} -return n-1th snippet
/{n} - returns nth snippet
/edit - opens an editor (good for creating classes)
/save -all {filename.sh} - store all the snippets
/open fn.jsh - open the filename snippet
REPL supports the following startup options.
DEFAULT,
JAVASE,
PRINTING
For eg:
jshell —startup DEFAULT —startup PRINTING
with that you can type the following instead of regular System.out.println(1+1)
print(1+1)
One good thing is about the optional ; though ;-)
To load the jars into Classpath use the below
jshell —classpath gson.jar (outside, when invoking jshell)
/env —classpath (inside jshell to load cp)
As a good shorcut, use the following to clear the contents of REPL
ctl_l — clear
You might wonder how REPL can be used for a regular Java Engineer.
But think of some scenario wherein you wanted to try a Regex for a string
Earlier the options are limited and lengthy and now its as breezy as below:
“abc”.matches(regex)
And by the way, to exit out of it, run “/exit”















