Java 9 JShell overview


Originally introduced as project Kulla, JShell is a great tool for new and experienced developers.

JShell is a read-eval-print-loop (REPL) tool which can be used to quickly interact with Java from the command line without the need to compile your code.

MacOS setup for JShell:

You can start up the JShell console via a terminal window session:

$ jshell

From there you can execute java statements. While it is possible to combine some statements into one line such as a for loop it is preferred practice to got with a more reactive programming style such as java streams. For example:

jshell> import*
jshell> IntStream.range(0, 10).map(i -> i * i).toArray()
$1 ==> int[10] { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }

After executing the above you can now use $1 in future expressions inside the current jshell session.

JShell is utilimately a great tool for users to explore the core Java API.

Printing with JShell

To support printing with JShell you need to run it with the print option:

$ jshell --start DEFAULT --start PRINTING

With support for print you can now execute statements like the following:

jshell> printf("Pi is %.5f\n", Math.PI)
jshell> println( "Print me!")

Without the jshell print options you will get an error that the method doesn’t exist. You can get around this by creating your own methods but it adds extra effort.


Its also possible to edit JShell in a separate java editor by typing:

jshell> /edit

If you have recently created a method in your session history you can extend this to edit that method:

jshell> void testMe() {
jshell> System.out.println("testing");
jshell> }
jshell> /edit 1

Alternatively you can switch to your favourite editor with:

jshell> /set editor /usr/bin/vim


It’s also possible to list any methods created in your current session:

jshell> /list

If you want to list the all of the current imports you can execute:

jshell> /imports

Save and Open

A useful feature in jshell is the save and open commands. If you need to save a snippet source to a file you can execute:

jshell> /save <FILE>

By default this will save all commands and snippets currently active in your session. There are several variations that you can use with this such as /save -all | -history | -start. Each of these will extract a slight variation of your commands which you can find more information on through the jshell help.

Conversely if you need to open files back up again you can execute:

jshell> /open [FILE]

To be honest I which this feature was a little more intuitive. In most cases you will really want to save either methods or one liners in which case it would seem better to me to have a separate set of commands for this. Also I would really like to automatically preload methods/functions for newly created jshell sessions but so far I haven’t found an ideal way to do this.


If you want to find out about other supported commands you can run:

jshell> /help


The preferred way to exit a jshell session is to use:

jshell> /exit


JShell comes with a number of convenient shortcuts designed to make the experience faster for advanced users.


1) Autocomplete variable assignment

If you create a new variable it will be automatically assigned to a $<NUM> variable. However you can assign this to your own variable name very quickly by holding down SHIFT + TAB followed by v.

jshell> new Random()

This will automatically pre-fill the expression for you and allow you to assign a varaible name of your choice:

jshell> Random <TYPE_YOUR_NAME> = new Random()

2) Find, choose and import

Type into jshell the name of your package or class and hold down SHIFT + TAB followed by i:

jshell> Duration
0: Do nothing
1: import: java.time.Duration
2: import: javafx.util.Duration
3: import: javax.xml.datatype.Duration

You can then type which of the above packages you want and it will be imported for you automatically. For example typing 1 for the list above will output:

Imported: java.time.Duration