Java 9 REPL

Introduction to Java 9 REPL

In this post, we will explore Java 9 REPL (Read-Evaluate-Print-Loop) feature or shortly called as JShell. Many modern languages provide a tool (Mostly called as REPL or scripting tool) for real-time statement interpretation.

One of the benefits of such tool is that you can easily test your code without creating a complete class or project.Java 9 will be introducing REPL or JShell which can be used to quickly run your code and compare results. 

This post assumes that you already have Java 9 installed on your machine, if this is not the case, install Java 9 on your machine before moving forward. 

REPL Concept is not new and some of the modern languages have provided this feature, here is the list

  1. Groovy Console
  2. Beanshell
  3. Scala

If we want to run some programme in Java (Java 8 or lower version), We need to create a project and the main method to run that code and any change require a recompile and repeat this process.

1. What is Jshell

Jshell will be shipped as part of the Java 9 and will work as scripting shell to run your Java code without the need of creating project or class with the main method.

To run Jshell, run following command with -v to get information about the version


localhost:~ umesh$ jshell -v
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> 

If you have Java 9 configured correctly, Jshell will greet you with a Unix style welcome message.

2. What is Jshell

Jshell comes with a certain set of default import. This indicates you don’t have you do explicit import for these to run you programme, to list default imports run /import command in Jshell


jshell> /import
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

jshell>

Jshell /import will give you a list of import for the current session, this means if you will add additional import and run this command again, those import will be added automatically by Jshell for the current session.

3. Simple Hello Word

Running and printing simple hello work using Jshell is quite simple and you don’t need to write a complete .java class for this.


jshell> System.out.println("Hello World from Jshell!!!");
Hello World from Jshell!!!

jshell>

Jshell is flexible in nature and you can even skip adding ; at the end of the statement and Jshell will handle it internally.


jshell> System.out.println("Hello World from Jshell!!!")
Hello World from Jshell!!!

jshell>

4. Creating and Running Method

Methods can be created and executed easily using Jshell


jshell> void helloWorld(){ System.out.println("Hello World!!");}
|  created method helloWorld()

jshell> helloWorld()
Hello World!!

jshell>

Jshell even allows you to modify your existing code, Let’s say we want to change “Hello World!!” to “Hello World from JShell”, you can easily change it.


jshell> void helloWorld(){ System.out.println("Hello World from JShell!!");}
|  modified method helloWorld()
|    update overwrote method helloWorld()

jshell> helloWorld()
Hello World from JShell!!

jshell>

5. Expressions

Jshell will accept any valid Java expression, Jshell will execute the expression, it will provide you information about the value, value type.


jshell> 3+7
$5 ==> 10
|  created scratch variable $5 : int

jshell> $5
$5 ==> 10
|  value of $5 : int

jshell>

Jshell provides you with a detailed information about the new variable ($5) it created and what is the value assigned to this new variable.You can even refer to this variable by just naming it $5

6. Variables

You can create variable and even name those variables using JShell, these variables will be visible in the current Jshell context and you can change/modify values as per your need.


jshell> int i=10
i ==> 10
|  created variable i : int

jshell> String blogName="Umesh Awasthi";
blogName ==> "Umesh Awasthi"
|  created variable blogName : String

jshell> blogName= blogName+" Java 9 REPL";
blogName ==> "Umesh Awasthi Java 9 REPL"
|  assigned to blogName : String

jshell> blogName
blogName ==> "Umesh Awasthi Java 9 REPL"
|  value of blogName : String

jshell>

7. Commands

Jshell provide number of build in command which can be used to get some insight on to the JShell, you can run these command on the JShell using forward slash (“\”) 


jshell> /vars
|    int $5 = 10
|    int i = 10
|    String blogName = "Umesh Awasthi Java 9 REPL"

jshell> /methods
|    void helloWorld()

jshell>

You can use /vars to list all variable in the current context, a similar way you can use /methods to list down all the method in the current context. Use /help to start Jshell help menu.

8. List

Jshell is quite powerful and it keeps trek of the activities happening in the current context, however, it’s not an IDE and you might want to get a list of all variables or methods or values being used in the context, Jshell provides /list method to give you all the information.


jshell> /list

   2 : helloWorld()
   3 : void helloWorld(){ System.out.println("Hello World from JShell!!");}
   4 : helloWorld()
   5 : 3+7
   6 : $5
   7 : int i=10;
   8 : String blogName="Umesh Awasthi";
   9 : blogName= blogName+" Java 9 REPL";
  10 : blogName

jshell> 

9. Save and Reload

Use /save method to save expression history, it will save a file in the same directory from which we are running the Jshell. To open saved file, we can use /open a command.


jshell> /save jshell.java
jshell> /open jshell.java

10. Forward reference

JShell provides a very good support for forward reference, this means we can refer to variable or methods which we are planning to introduce later in our code.

code class="language-vim">
jshell> double totalPendingAmount(int customerNumber){ return getCustomerPendingAmount(customerNumber);}
|  created method totalPendingAmount(int), however, it cannot be invoked until method getCustomerPendingAmount(int) is declared

jshell>

The interesting part is the output of above command, JShell indicating that we will not be able to use this method until getCustomerPendingAmount(int) is defined.

11. JShell API

Jshell also provides API which can be used by external parties to use Jshell capabilities. 

12. Why use Jshell

We are going to have a very natural question “Why use Jshell ??” , I will try to come up with some use cases where it will really be handy to use Jshell 

  1. JShell API provides a way to have a network connection, We can use it to connect to our remote server and may change few things remotely.
  2. We can even connect to DB and perform some operations.
  3. API can be used to hook into live JVM’s to get an insight of it.
  4. We can use JShell to determine and verify return type (Check video for detail)
  5. Rapid prototyping, You can quickly modify and run your programme without waiting for multiple rebuilds or redeploy.

13. Video Tutorials

It is always interesting to see things in action. Please watch this video to see Jshell in action.

Conclusion

Jshell is really interesting and powerful tool for rapid prototyping. I believe that Jshell will become a friendly tool for developers on a day to day life. Jshell has a lot of use cases but the best one for me is the ability to quickly test your code without getting into long build cycles.

Read Collection Factory Methods in Java 9  to learn how to create immutable collection in Java 9.

JavaDevJournal

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

Leave a Reply

3 Comments on "Java 9 REPL"

Notify of
avatar
Sort by:   newest | oldest | most voted
Yoav Naaman
Guest

From security POV, can someone read/modify my JVM running process? or control a running process?

Umesh Awasthi
Guest

It just provides you with a way to connect remote system or your system but underlying security mechanism will be same.

wpDiscuz