JShell is the interactive command-line tool for the Java programming language. It was officially introduced starting with Java 9 (JDK 9) and is a REPL (Read-Eval-Print Loop) environment.

It’s ideal for quickly testing the functionality of a Java API method, a regular expression, a new language feature, or a syntax you’re not sure about. For example, you can test a lambda expression or a new Streams API method without creating a complete project.

allows you to see the immediate result of each line of code


The concept of REPL means that the program works in a continuous loop:

  • Read: Reads the line of Java code you enter.
  • Eval: Executes that code immediately.
  • Print: Displays the result of the execution.
  • Loop: Returns to the beginning, waiting for the next input.

I use maven, which installs .jar or .war as artifacts online (sonatype or maven central) and locally (.m2/repository/), maven first looks locally if an artifact is installed, then online

Before using JShell you must compile and install your code.

mvn compile

install the artifact locally

mvn install

The current library you are creating and the dependencies are stored in .m2/repository/

.m2 is a hidden directory

.m2 is available on macOS and Linux.


I have SETAVL a library that I am creating, SETAVL uses AVLTree, AVLTree is a dependency

The libraries you are creating and their dependencies are stored locally in .m2/repository/

The location of .m2/repository/ is ~/.m2/repository/

The location of the .jar by .m2 are ~/.m2/repository/{group_id}/{artifact_id}/{version}/{file.jar}

My group_id is com.leibnix, translated into directories it is /com/leibnix/

~ is the home directory of the current user

You must tell jshell the library and its dependencies (all the .jar files involved)

The routes are enclosed in quotes (“) and separated by(:)

jshell --class-path "library.jar:dependency1.jar:dependency2.jar"

Another location for .jar files is /target in your project, but that’s more temporary than ~/.m2/repository/

example:

jshell --class-path "~/.m2/repository/com/leibnix/setonavl/1.0.0/setonavl-1.0.0.jar:~/.m2/repository/com/leibnix/avltree/1.0.1/avltree-1.0.1.jar"


That command above allows you to access JShell and you shouldn’t see any errors.

Inside JShell, you need to import your library (same as in Java)

import com.leibnix.SETAVL;

then you need to create an instance of what you want to test

SETAVL a = new SETAVL();

then you print the result of the method you want to test

System.out.println(a.toString());


test image in Jshell:

“a ==> 1” is because jshell Implicitly Calls toString()

When you enter any expression or statement in JShell (such as creating an object, performing a mathematical operation, or calling a method), JShell does two things:

  • Evaluate the code
    • SETAVL a = new SETAVL();
  • Show the result
    • Evaluate the resulting variable (a) to print its value
  • JShell doesn’t always print the reference (@hashcode). Instead, JShell internally calls the object’s toString() method and displays the string that method returns
  • JShell Evaluates a: JShell internally executes something similar to a.toString()
  • then you explicitly call a.toString(), and System.out.println prints the result, which is “1”.

“toString()” method in the SETAVL class


JShell uses special commands that begin with a forward slash (/) to manage the environment

Exit the JShell session

/exit

Displays help and a list of available commands

/help 

Displays the classes that have been imported (including default imports like java.util.*)

/imports

Displays the history of all the code (statements and methods) you have entered into the session

/list 

Resets the JShell environment to its initial state, deleting variables and methods

/reset