Java 9 – JShell

Lokesh Gupta

JShell is new command line interactive REPL (Read-Evaluate-Print-Loop) console shipped with JDK 9 distribution [JEP 222] to evaluate declarations, statements and expressions written in Java. JShell allows us to execute Java code snippets and get immediate results without having to create a solution or project.

In this tutorial, we will learn about various tasks, we can do in JShell, with examples.

Table of Contents

1. Launch JShell
2. Write and Execute Java Code
3. Edit Code in JShell Edit Pad
4. Launch Code in External Editor
5. Load Code from External Files

1. Launch JShell

The very first thing is to have JDK 9 installed into your machine. Download JDK 9 from this link, and install it.

Go to installation location and look into /jdk-9/bin folder. You will find the jshell.exe file in here.

JShell location in JDK 9
JShell location in JDK 9

Now launch a new command window and check the java version.

>> java -version

It should point to JDK 9 version. If it is not then update environment properties JAVA_HOME and PATH with corresponding values.

JAVA_HOME=C:\Program Files\Java\jdk-9
PATH=C:\Program Files\Java\jdk-9\bin	//Path till bin folder

Now again launch new command prompt window and type command jshell. It will change the cursor to jshell.

Jshell Launched Window
Jshell Launched Window

Congratulations, you are ready to play in JShell REPL (Read-Eval-Print Loop).

2. Write and Execute Java Code in REPL

Jshell allow to create small code snippets and test them without requiring to create and build a complex project. And that’s how it should be used. Working on JShell is kept easy to make it usable and fast. Let’s see how?

2.1. Variables

You can define variables just like you do in real programming. Only difference is that you don’t have to write a class or main method to start with.

jshell> int i = 10;
i ==> 10

To print the value of variable, just type the variable name and hit ENTER. It will print the value of variable.

jshell> i
i ==> 10

To reassign a variable to new value, simply do it as normal way.

jshell> i=20;
i ==> 20

To list all the declared variables, use command /vars.

jshell> /vars
|    int i = 20
|    int j = 30
Working with Variables in JShell
Working with Variables in JShell

2.2. Methods

Much like variables, methods are also straightforward.

To create method in jshell, define method with retrun type, method name, parameters and method body. Access modifiers are not required.

jshell> int sum (int a, int b) {
   ...> return a+b;
   ...> }
|  created method sum(int,int)

To list all defined methods, use command /methods.

jshell> /methods
|    int sum(int,int)

To call the method, call it like normal programming.

jshell> sum(2,2)
$6 ==> 4

If you want to view the method code, use /list command. It will show the current method sourcecode. T

jshell> /list sum

1 : int sum (int a, int b) {
   return a+b;
   }

To change the method code, you will need to rewrite the new modified code, with same method name.

jshell> int sum (int a, int b) {
   ...> int c = a+b;
   ...> return c;
   ...> }
|  modified method sum(int,int)

jshell> /list sum

   3 : int sum (int a, int b) {
       int c = a+b;
       return c;
       }
Working with Methods in JShell
Working with Methods in JShell
Please keep in mind the method overloading rules. It you changed the method parameters count or their data types, then it will be a new method and there will be two methods registered in jshell.

3. Edit Code in JShell Edit Pad

By the time, you are working in few lines of code, JShell inline editor is good enough. But when you code start getting bigger then you might need a file editor to modify your code.

Here you can use JShell edit pad. To launch edit pad, use /edit command with method name.

JShell Edit Pad
JShell Edit Pad

Here change the method code as you want and click on Accept button. Modified code will be updated in Jshell and you will get confirmation message in prompt. You can change code as many time times as you like, save it and then exit the window.

Save Operation in Jshell Edit Pad
Save Operation in Jshell Edit Pad

4. Launch Code in External Editor

Edit pad is really good enough for most of the needs, still if you like to code on any particular editor then you can use it as well. JShell allows to easily configure any external editor to edit the code snippets. You just need to get the complete path to the editor we want to use and run /set editor command in JShell to configure the editor.

/set editor "C:\\Program Files\\Sublime Text 3\\sublime_text.exe"

Now execute the /edit command again. Now it will open the code in sublime editor.

Launch Sublime Editor from JShell
Launch Sublime Editor from JShell

Feel free to edit the code and save you did in edit pad.

5. Load Code from External Java File into REPL

Many times, you will have some code already written in any java file and you would like to execute it into JShell. To load file in JShell, use /open command.

Let’s say I have one file Demo.java in c://temp folder. It’s content is:

int i1 = 10;
int i2 = 20;
int i3 = 30;
int i4 = 40;

int sum(int a, int b) {
	return a+b;
}

int sum(int a, int b, int c) {
	return a+b;
}

Now let’s load the file in JShell.

/open c:\\temp\\demo.java

Verify the variable and methods loaded in Jshell.

Java Code loaded in JShell
Java Code loaded in JShell

That’s all you must know while working with REPL tool in Java 9.

Drop me your questions in comments sections.

Happy Learning !!

Comments

Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode