JShell : JAVA 9 REPL Tool

JShell is a command line tool to use of Java Programming Language elements. JShell is a REPL (Read-Evaluate-Print-Loop). It is introduced in JDK 9.

Starting JShell

JShell is included in jdk 9, so first install jdk 9. To launch the JShell tool, just type jshell at the command-line.

jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

To exit type /exit or ctrl+D.

jshell> /exit
|  Goodbye

Try It

> System.out.println("Hi");
Hi                                                                     
>
> int a = 10;
a ==> 10

> int b = 12;
b ==> 12

> int c = a+b
c ==> 22
> 2 + 2
$1 ==> 4
|  created scratch variable $1 : int

> System.out.println($1);
4

Also, you can create class, invoke methods using command line.

> class Tester {
   ...> public static void test(){
   ...> System.out.println("Hello World");
   ...> }
   ...> }
|  created class Tester

> Tester.test();
Hello World

For help of JShell use /help command.


> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|  /list [<name or id>|-all|-start]
|       list the source you have typed

|  /edit <name or id>
|       edit a source entry referenced by name or id

|  /drop <name or id>
|       delete a source entry referenced by name or id

|  /save [-all|-history|-start] <file>
|       Save snippet source to a file.

|  /open <file>
|       open a file as source input

|  /vars [<name or id>|-all|-start]
|       list the declared variables and their values

|  /methods [<name or id>|-all|-start]
|       list the declared methods and their signatures

|  /types [<name or id>|-all|-start]
|       list the declared types

|  /imports
|       list the imported items

|  /exit
|       exit jshell

|  /env [-class-path <path>] [-module-path <path>] [-add-modules <modules>] ...
|       view or change the evaluation context

|  /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]...
|       reset jshell

|  /reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]...
|       reset and replay relevant history -- current or previous (-restore)|  /history
|       history of what you have typed

|  /help [<command>|<subject>]
|       get information about jshell

|  /set editor|start|feedback|mode|prompt|truncation|format ...
|       set jshell configuration information

|  /? [<command>|<subject>]
|       get information about jshell

|  /!
|       re-run last snippet

|  /<id>
|       re-run snippet by id

|  /-<n>
|       re-run n-th previous snippet
|

|  For more information type '/help' followed by the name of a
|  command or a subject.

|  For example '/help /list' or '/help intro'.

Checkout all features of JShell in Official Documentation.

2 thoughts on “JShell : JAVA 9 REPL Tool

  • October 3, 2017 at 8:28 pm
    Permalink

    I see you don’t monetize your page, don’t waste your traffic, you can earn extra bucks every month because you’ve got hi quality content.
    If you want to know how to make extra $$$, search for: Mrdalekjd methods for $$$

    Reply

Leave a Reply

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