JShell is a Java tool shipped with JDK 9. It provides a command-line interface to interact with the Java platform. We can write quick code snippets and get their results in an interpreter style. It works on the REPL (Read Evaluate Print Loop) principle. It is, however, more than just a console and can be used as an API to develop a program that features functionality as itself. This article provides an overview of this much needed excellent tool introduced with Java 9.
Overview of JShell
Unlike many other languages—such as Python, Ruby, Clojure, Lisp, or Groovy—Java had been lacking an REPL interface from a long time. Even the simplest Java code had to go through the rigor of editing, compiling, and then execution. There was no simple way to see what a rudimentary code did without resorting to writing a complete program. Here is a program to print a string in the simplest and quickest possible way in modular fashion in Java 9:
module MyProject { } package org.mano.java9.examples; public class Main { public static void main(String[] args) { System.out.println("print this..."); } }
The possibilities change completely with JShell. Here, we can write a segment of a code as simple as what’s shown in Figure 1 and still get the same output.
Figure 1: A simple segment of code
JShell is particularly good in a situation where a code snippet needs to be executed either to demonstrate a point while teaching or to quick test Java code syntactically or semantically. This also leverages the idea of incremental prototyping, such as the building of one code segment upon another. The essence is that Java never had such facility until now and the possibilities of its usages are immense.
Here is an example code snippet and its output executed in JShell.
Figure 2: Sample code and its output
How JShell Works
Note that JShell is just a REPL tool and has nothing to do with the new language features of Java or the compiler. It supports all the API of the underlying language. The Java compiler, however, does not understand code snippets. This means that any code which is signalled as error-free by the compiler must be submitted as a complete Java program. JShell is exception in this regard and lets one write only a subset of code and it still gets compiled by Java. JShell is armed with a parser that parses the code submitted to it and determines its types such as a loop, variable, a method declaration, and so forth, and wraps them into a dummy code that makes it a complete Java program and submits it to Java compiler. The compiler accepts the input and converts it in to bytecode. Because no file is created in-between, everything takes place in memory. The generated bytecode then is given to JVM for loading and execution.
Starting JShell
Java 9 ships JShell with its JDK. It is located in the bin directory of the JDK9 installation directory (see Figure 3).
Figure 3: Locating JShell
Open a terminal, go to the bin directory, and type the following command:
./jshell
If the bin directory is set to the PATH environment variable, a simple
jshell
command in the terminal will start it.
Figure 4: Starting JShell
To exit from JShell, either type /exit or /ex (the first two letters of the exit command). The command to open up an external editor is /edit or /ed.
The /help intro provides the following information. There are several other commands that we can list with the /help command. Some of the important ones are discussed below.
Figure 5: Viewing help
The /imports command shows a list of default Java import packages, such as shown in Figure 6:
Figure 6: Result of typing the /imports command
We can custom start JShell by using the --start option, such as:
jshell --start DEFAULT --start PRINTING
This means that if we want to start up JShell with default import classes with the DEFAULT option and PRINTING options, we no longer need to write an elaborate System.out.print("Hello"); to print in the console. Instead, we can write println("Hello") concisely; that’ll do the trick.
Working in JShell
JShell may be used to evaluate mathematical calculations in a very simple manner. It is not compulsory to terminate every expression with a semicolon. However, one may do so out of habit; no harm there. For example, the expressions 8*7 and 8*7; are basically same in JShell, as shown in Figure 7.
Figure 7: Use of the semicolon is optional
Observe that the variable names, such as $1, $2..., are automatically generated by JShell and their types also are intelligently determined. We can easily find out the type of the variable in JShell with the help of the /vars command. The /list command shows the list of expression entered so far (see Figure 8).
Figure 8: Using the list command
Similarly, we can do a Java String operation in JShell as follows:
Figure 9: Performing a Java String operation in JShell
Suppose that one is not sure about what methods are available with the string object $1. Just press . (dot) and then TAB. It will list out all the available methods (see Figure 10). Also, typing the first few letters of a function name and then pressing the TAB key twice will give an option to open up the Java API documentation in the console. Try typing:
jshell> "Hello".toU <TAB> <TAB>
Figure 10: Listing the available methods
Take this idea a step further; type any capital letter and then press the TAB key. This will show an alphabetical list of classes and interfaces of the imported Java API library.
Figure 11: Generating an alphabetical list of classes and interfaces
Declaring a variable in JShell is as simple as in normal Java programming. Here is an example that prints the Fibonacci series in JShell:
Figure 12: Printing the Fibonacci series in JShell
Conclusion
Perhaps the greatest usage of JShell is to check quick code snippets and their execution in the shortest possible time. The tool will be very helpful for every programmer. In the preceding glimpse of its features, it is clear that JShell is packed with many new tricks that we can take advantage of. Many of these will be taken up in subsequent articles, so stay tuned.