Java 9 JShell Basics

In this tutorial we will go though some of the basics of the new Java 9 JShell feature. JShell stands for Java Shell tool, its an interactive tool to learn java and for quick prototyping. Its a command line tool which comes with Java 9.

Starting Java 9 JShell:

To start JShell you will need to have installed Java 9 on your machine. Once that is done you will need to be in the /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin directory (on Mac) or add it to your PATH and then execute the following command.

Declaring variables:

To create a variable its simply doing the following:

As you can see from the example above we have declared some variables and also used them in a println statement.

Creating methods:

JShell also allows you to declare and use methods.

As you can see from the example above we can declare and use the method. If you provide an invalid input an exception will be thrown:

Java 9 JShell commands:

JShell has a list of useful commands to help you below is a subset:

As you can see these commands give you an insight into which variables, methods and imports have been defined in your JShell session, to exit your session simple type /exit

I hope you found this post useful and helps you understand how to use JShell a little bit better.

Similar posts:

  1. Java 9 Collections factory method


  1. Introduction to JShell

Leave a Reply

Your email address will not be published. Required fields are marked *