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
jshell
|  Welcome to JShell -- Version 10.0.2
|  For an introduction type: /help intro

jshell>

 

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
Signatures:
greeting:java.lang.String

 

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.

jshell> greeting.to
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");
|  ^--------------^

 

Imports

By default following imports are available in the JShell.

  • java.io.*
  • java.math.*
  • java.net.*
  • java.nio.file.*
  • java.util.*
  • java.util.concurrent.*
  • java.util.function.*
  • java.util.prefs.*
  • java.util.regex.*
  • java.util.stream.*

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);
A
B
C

 

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.

Conclusion

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

References

Java 10 JShell – https://www.techtalks.lk/blog/2018/9/java-10-jshell

Author : Admin
Published Date September 28, 2018
Share Post