jshell: The Java Shell (Read-Eval-Print Loop)

Java 9

                         In this article, we will discuss jshell(Java Shell) Java 9 feature. We can explore jShell with JDK 9 Early Access Release.  As of now the general availability of JDK9 is scheduled to 27th July 2017. The jShell feature is proposed as part of JEP 222. The motivation behind jshell is to provide interactive command line tools to explore the features of Java quickly. It is a very useful tool to get a glimpse of Java features very quickly for new learners. Already Java is incorporating functional programming features from Scala. In the same direction, they want REPL(Read-Eval-Print Loop) interactive shell for Java as Scala, Ruby, JavaScript, Haskell, Clojure, and Python.

                       The jshell the tool will be a command-line tool with features like a history of statements with editing, tab completion, automatic addition of needed terminal semicolons, and configurable predefined imports.

After downloading the JDK 9, set the PATH variable to access jshell. Now, we will see how to use jshell. Below is the simple program using jshell. We no need to write a class with public static void main(String[] args) method to run a simple hello world application.

| Welcome to JShell — Version 9-ea
| For an introduction type: /help intro
jshell> System.out.println("Say hello to jshell!!!");
Say hello to jshell!!!

view raw


hosted with ❤ by GitHub

Now we will write a method which will add two variables and invoke the method via jshell.

jshell> public class Sample {
…> public int add(int a, int b) {
…> return a+b;
…> }
…> }
| created class Sample
jshell> Sample s = new Sample();
s ==> Sample@49993335
jshell> s.add(10,9);
$4 ==> 19

view raw


hosted with ❤ by GitHub

Now, we will create a static method with StringBuilder class without importing it, as jshell does that for you.

jshell> public class Sample {
…> public static void join() {
…> StringBuilder sb = new StringBuilder();
…> sb.append("Smart").append(" ").append("Techie");
…> System.out.println("The string is " + sb.toString());
…> }
…> }
| created class Sample
jshell> Sample.join();
The string is Smart Techie

view raw


hosted with ❤ by GitHub

I hope you enjoyed jshell feature. In the next article, we will see another JDK 9 feature. Till then, Stay Tuned!!!


Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

Tagged with: , ,
Posted in Java
3 comments on “jshell: The Java Shell (Read-Eval-Print Loop)
  1. Raghupathi says:

    nice one….

  2. autumnator says:

    How would you say this compare to Beanshell?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.



Java Code Geeks
Java Code Geeks
%d bloggers like this: