Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Getting Started with JShell Part 1

Learn the basics of JShell, a Read-Evaluate-Print-Loop command line tool, including executing simple statements, defining classes, and commands to manage code snippets.

Dec 14, 2018 • 5 Minute Read

Introduction

Imagine you have been given a new library and you want to try it to see how it works.

What would you do?

Would you create a class with a main method, in a new or an existing project just to test it?

A JUnit test?

Well, in Java 9, you have another option to accomplish this task: JShell.

JShell is a Read-Evaluate-Print-Loop (REPL). This is a command line tool that allows you to enter Java statements (simple statements, compound statements, or even full methods and classes), evaluate them, and print the result.

In this guide, you'll learn the basics of working with JShell, from executing simple statements to defining classes, along with some commands to manage code snippets. Finally, we'll go through a practical example that will show you how to use JShell to explore Guava, a library that includes many utilities and collection types for Java.

JShell comes with the Java 9 JDK distribution. You can download it from here or here and follow the installation instructions. Alternatively, you can use Docker and an OpenJDK image to run JShell.

The Importance of a REPL

In a few words, a REPL provides a simple way of executing code.

You might be thinking, why is a REPL better than using other methods? Why not create a class with a main method in an IDE?

Besides, you could say that Java already has a few third-party REPL tools (like Java REPL), without mentioning the capability of some IDEs to execute snippets of code.

The JDK Enhancement-Proposal (JEP) for JShell provides two excellent reasons. Here's the first one:

  • Immediate feedback is important when learning a programming language and its APIs. The number one reason schools cite for moving away from Java as a teaching language is that other languages have a "REPL" and have far lower bars to an initial "Hello, world!" program.

When you're learning something new, it's important to try out things that require minimal effort and have immediate feedback. Previously in Java, if you had to try out some code, even as basic as Hello World, you had to create a class, add a main method, add lines of code, perhaps add dependencies, compile, and (if there are not any errors) run the code. Even with an IDE simplifying things, that's more complicated than using a REPL.

Of course, Java has alternatives to JShell, but they don't have all the features that JShell provides in one package, like full language support or preserved context and session history.

In addition, JShell also provides an API to its evaluation engine, so it can be integrated to IDEs or other tools to further simplify our workflow.

Even experienced developers can benefit from a REPL. Here's the second reason:

  • Exploration of coding options is also important for developers prototyping code or investigating a new API...Without the ceremony of class Foo { public static void main(String[] args) { ... } }, learning and exploration is streamlined.

You can quickly prototype and experiment with libraries (what's is the return type of a method?), as well as language features (what's the format pattern to show the time zone for a date?).

Having explained the importance of a REPL, let's dig into the basics of JShell.

Installation

As stated before, JShell is included with the JDK. Just go to the bin directory of your installation, for example:

      cd C:\Program Files\Java\jdk-9\bin
    

Alternatively, you can add this directory in your PATH environment variable.

Next, execute:

      jshell
    

Shell interactions

You should see something like the following:

      $ jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell>
    

This will start an interactive stateful session, which means that it will remember previously defined statements and modifications in the same session.

You can input specific JShell commands or Java code (referred as snippets), in particular:

  • Expressions
  • Statements
  • Class declarations
  • Interface declarations
  • Method declarations
  • Field declarations
  • Import declarations

At this point, everything is set up to start working with JShell.

Next Steps

In the next guide in this series, you'll learn how to work with expressions, variables, classes, and more.