JShell – Explore the Java 9 interpreter

JShell is a new interactive command line tool for accessing the Java language. It is introduced in Java 9. Working with JShell is similar to using the python interpreter via commandline. These tools can be collectively called as REPL (Read-Eval-Print-Loop) because they take inputs, evaluate and returns output to the user via command line. You can also use JShell as an API within other applications and code to the API to implement further enhancements in the application.


JShell comes with jdk 9 which will have a General Access release in July 2017. As of now, the early access meant for testing the application is available at : http://jdk.java.net/9. Once installed, change JAVA_HOME to the Java 9’s JDK installation folder. If path is set correctly, java -version should point to jdk 9-ea.

Running JShell

JShell can be accessed via command prompt. Currently, to allow printf() to be used with ease, you need to invoke jshell via command prompt in following manner:

The command above imports all the built-in printing methods. Simple print statement can be thus invoked as:

JShell Commands

JShell provides a list of commands that could be used for snippet manipulation. We will cover important commands one by one.


Gives access to help menu. Further help on a particular command can be accessed by typing:


Lists all the snippets that we have typed into the particular jshell session.


Gives history of commands that we typed in. While /list will list only the successfully run programming snippets alongwith the snippet number, /history lists everything that we entered into the console so far since the start of the jshell session:


Used to exit the JShell session.


While working on a prototype, often we would like to save current work and close jshell session so that we can resume work from that point after opening the session again. For this backup process /save command is used.


Once the backup is added, we may want to restore the saved session. This is done using open command.


Reruns the last snippet


Rerun the snippet with the mentioned snippetid.


Rerun the nth snippet from the last


Lists down all imports that are available.


Lists all the variables declared with their values.


Lists all the declared as well as imported methods.

Here, all the print statements were automatically imported as we gave –start PRINTING as a command line parameter to jshell during invocation. Notice that the new function created is added to end of the list.


Used to edit a particular snippet already added to the list in JShell.

In the above case, a new function is created as the method name itself is changed. We will change the variable name from n to x. Notice the changes in definition of isOdd before and after modification.


Drop a snippet by its id from the list.

Thus, we have covered most of the major built-in commands of JShell.

Running A Whole Java File As A Snippet

Consider a simple Java class with a main function.

This can be added as a java snippet and executed in jshell:

Note that TestSnippet.main() always executes the main method. /15 in last jshell session reruns the method getTicketNumber().

Integrating JShell With Other Applications

It is possible to create a new JShell instance programmatically using Java. The JShell and its associated APIs can be found under the package jdk.jshell. There is a proposal to incorporate this in various IDEs like Eclipse and NetBeans. This is some work that is in progress and may be completed only after GA release of Java 9.

Let us learn how to program with JShell API with a small example:

In the above program, we get a new instance of state engine for Jshell programmatically using the static function create().

Jshell.eval() is used to add an expression to the Jshell instance. The eval() method returns a list of events that are triggered by the evaluation. The evaluation should be exactly one snippet which can be an expression, a statement, method, class or variable declaration or an import statement. For a method, class or variable declaration that is added as an input for this eval statement, modifiers like public, protected, private, static and final are not allowed. If there was a previous definition with same signature, then that will be overwritten with the current one. The Status of SnippetEvent will be changed to OVERWRITTEN.

Each SnippetEvent created from the eval method can be checked for the output of the expression using SnippetEvent.value().

Output of the above program:


Jshell is an exciting addition to the Java. And should help Java to be accepted in the educational institutions. Learning of programming languages usually starts with languages that are easy to execute and don’t need any boilerplate coding to execute a simple print statement. Jshell brings the REPL to Java and this should help the language in future.

Leave a Reply