
JShell

satzCalvin Blog
Java JShell Tutorial with Examples
JShell is new command line interactive tool 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
Launching JShell
Writing Java Code
Edit Code in JShell Edit Pad
Launch Code in External Editor
Load Code from External Files
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.
>> 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.
Congratulations, you are ready to play in JShell REPL (Read-Eval-Print Loop).
Writing Java Code
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?
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
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;
}
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.
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.
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.
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.
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.
That’s all you must know while working with JShell REPL tool in Java 9.
Happy Learning !!