Java 10 Shell

Author: Shazin Sadakath

Java 9 Introduced Java Shell or JShell which is a Read-Evaluate-Print-Loop (REPL) tool which can be used for prototyping, debugging and learning Java, Java API interactively using a shell. This eliminates the dummy test classes needs to be written with a Java main method, compile it and run it. Let's have a look at some of the use cases for this.

Getting Started

The JShell can be invoked by issue command jshell on a Unix, Linux, Mac or Windows terminal.

> jshell
|  Welcome to JShell -- Version 10.0.2
|  For an introduction type: /help intro


Which will open an interactive shell which can take in Java syntaxes and give outputs accordingly. Lets just define some variables by the names greeting and object.

jshell> var greeting = "Hello";
greeting ==> "Hello"

jshell> var object = "World";
object ==> "World"

Now we can concatenate those two and print to the screen.

jshell> System.out.println(greeting + " " +object);
Hello World

Supports Tab Completion

After the start of the variable name pressing the tab key will complete the variable name.

jshell> gree

jshell> greeting

Pressing tab key twice will show the variable type and Java documentation if required by pressing tab key thrice.

jshell> greeting

Using Methods of Variables

Since greeting is a String variable we can use the methods available on it. These can also be accessed using the tab key.

toCharArray()   toLowerCase(    toString()      toUpperCase(
jshell> greeting.toUpperCase();
$4 ==> "HELLO"

Error Information

The REPL shell will show errors descriptively with the line no as below.

jshell> greeting.indexof("H");
|  Error:
|  cannot find symbol
|    symbol:   method indexof(java.lang.String)
|  greeting.indexof("H");
|  ^--------------^


By default following imports are available in the JShell.

And you can have additional imports if you want. Example you can use java.util.Arrays class as below

jshell> Arrays.asList('A', 'B', 'C').forEach(System.out::println);

Custom Methods

You can define your own custom methods and call them in the JShell as below:

jshell> public Integer random(int start, int end) {
   ...> return new Random(System.currentTimeMillis()).nextInt(end) + start;
   ...> }
|  created method random(int,int)

jshell> random(1, 10);
$7 ==> 9

jshell> random(1, 10);
$8 ==> 9

jshell> random(1, 10);
$9 ==> 10

Custom Classes

You can also define your own custom classes and use them in JShell as below:

jshell> class Calc {
   ...> public int add(int a, int b) {
   ...> return a + b;
   ...> }
   ...> public int substract(int a, int b) {
   ...> return a - b;
   ...> }
   ...> }
|  created class Calc

jshell> new Calc().add(1, 2);
$11 ==> 3

Static methods can also be defined and can be called without instantiating the class. 


This is a small summary of the capabilities of the JShell tool but it has much more broader and advanced features which can be learnt by going through the Oracle official documentation.


Tags: Java10 JShell Shell REPL
Views: 542
Register for more exciting articles


Please login or register to post a comment.

There are currently no comments.