What is JShell used for?

Introduction to JShell

JShell or the JAVA Shell is a REPL tool accessible through the command line. REPL is an abbreviation for Read, Evaluate, Print, and Loop and evaluates and produces an output on the console immediately when you execute your Java code.

Why JShell is useful?

JShell is so useful because it allows you to test out the functioning of individual statements of your JAVA code. This is especially useful when working with a new API or functions you are unfamiliar with; you can easily paste the program elements and other involved lines of code into the JShell. It will show you the output for those lines of code. This helps in making quick adjustments to your code in addition to testing the behavior of unfamiliar code.

Using the Jshell

JShell comes integrated with JAVA version 9. You can check if you have it by using the following command in your terminal:

java --version

It produces an output similar to this:

root@educative:/# java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.14.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.14.04, mixed mode, sharing)

As we can see, the version printed out is 11.0.11, which is greater than 9; this means we can run JShell.

With that out of the way, the only thing we need to do to run JShell is to run the following command in the terminal:

jshell

Now you will be able to enter the code you want to test out, and JShell will immediately tell you the output.

And when you are done working on JShell, you can run the following command to exit it:

/exit

Examples

In the following example, we run a simple one-line code that prints out “Hello world.”

Click on the terminal widget below to execute the example

Terminal 1
Terminal
Loading...

The code statements jshell can run are not just restricted to a single line. You may use Shift + Enter to move on to the second line, which will be displayed with an arrow ...> preceding the code.

Following is another example, here we are printing the numbers 0 to 4 using a for loop:

Terminal 1
Terminal
Loading...

As you can see above, we placed the for loop’s body on the next line, and the code was executed with numbers from 0-4 being printed in subsequent lines.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved