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.
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:
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:
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.
As stated before, JShell is included with the JDK. Just go to the
bin directory of your installation, for example:
1cd C:\Program Files\Java\jdk-9\bin
Alternatively, you can add this directory in your
PATH environment variable.
You should see something like the following:
1$ jshell 2| Welcome to JShell -- Version 9 3| For an introduction type: /help intro 4 5jshell>
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:
At this point, everything is set up to start working with JShell.
In the next guide in this series, you'll learn how to work with expressions, variables, classes, and more.