Java Shell Tool(JShell)

JShellJShell CommandsREPL

JShell is a tool for learning java. It is an interactive tool. JShell provides a read evaluate print loop (REPL). It evaluates the statements, expressions, etc and displays the result immediately. JShell runs one statement at a time. It runs one statement and prints the output. Due to this we can even do variations in our statements by looking to the previous output. JShell is run from the terminal. Running JShell is very easy, you just have to write jshell in your terminal and JShell will start.

$ jshell
...

To do code is very easy here. Now suppose if we want to type a string "Java Short Kicks". In Java we had to write a class and main() method, then compile and run, after doing all this then code gets run. But in Jshell you don't have to do this much, you just have to type the string in "".

...

If you want to do calculation, then simply write 3 + 4 it will give you the output.

...

If you want to calculate the length of the string then you can do by the following "Java Shortkicks".length()

...

If you what to write some expression and in that expression if you want to use result of the previous statement then you can do it by the following:

jshell> 5 * $1

/* Output */
$2 ==> 80
JShell String Example

In the above example, $2 is like a location where we have calculated the sum, the above expression will give the result as 105 i.e. we have given the value 15 it is multiplied by the value of $2 i.e. 7. We can also use variables to store the values.

jshell> int sum = 57 + 9

/* Output */
sum ==> 66

To exit JShell, we need to enter /exit on terminal:

jshell> /exit

JShell Command's

All Jshell commands start with forward slash(/). JShell provides various commands to update environment, current code variables information, methods information, and types information. Now let's check some basic commands:

/help — Shows commands and their arguments.

jshell> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|  /drop
|  	delete a source entry
|  /edit
|  	edit a source entry
...

/vars — Shows current variables information.

jshell> /vars
|    int $1 = 16
|    int $2 = 80

/methods — Shows current methods information.

jshell> void main(String[] arg){
  ...> System.out.println("ABC");
  ...> }
|  created method main(String[])

jshell> main(null)
ABC

jshell> /methods
|    void main(String[])

jshell>

/types — Shows current types information.

jshell> /types
|    class ABC

/list — Shows list of entered snippets information.

jshell> /list

1 : "zzz"
2 : String str = "zzz";
3 : class ABC {}
4 : void main(String[] arg){
    System.out.println("ABC");
    }
5 : main(null)

/import — Shows current import packages.

jshell> /imports
|    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.*

/list -all OR /list -start — Shows default startup script.

jshell> /list -all

s1 : import java.io.*;
s2 : import java.math.*;
s3 : import java.net.*;
s4 : import java.nio.file.*;
s5 : import java.util.*;
s6 : import java.util.concurrent.*;
s7 : import java.util.function.*;
s8 : import java.util.prefs.*;
s9 : import java.util.regex.*;
s10 : import java.util.stream.*;
 1 : "zzz"
e1 : print($1);                //e1(e) - error line
e2 : print(t);
e3 : t = "zzz"
 2 : String str = "zzz";
e4 : print(t);
e5 : import

     /methods
 3 : class ABC {}
e6 : ABC
e7 : printf("ABC")
e8 : main(null)
 4 : void main(String[] arg){
     System.out.println("ABC");
     }
 5 : main(null)

/**Press tab key OR /list -**Press tab key — Tab key shows all possible commands and command arguments.

jshell> /**press tab key**
/!          /?          /drop       /edit       /env        /exit
/help       /history    /imports    /list       /methods    /open
/reload     /reset      /save       /set        /types      /vars

jshell> /list -**press tab key**
-all       -history   -start

Command Abbreviations

JShell provide all unique commands abbreviated. For example we can use the /l -a abbreviations to enter the /list -all command.

/se abbreviation for /set.

/he abbreviation for /help.

/op abbreviation for /open.

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.