How to display different list commands in JShell in Java 9?


JShell has introduced in Java 9 and is a command-line tool that allows us to enter simple statements, expressions, methods, and classes without a main () method.

When we can enter code in JShell, the code has assigned a unique ID. This ID starts at 1 and has incremented for each command entered in JShell. The same can be true for libraries loaded at startup. For each of these imports, a unique ID has been assigned. It starts with $1 and is incremented for each code loaded ($2, $3 and etc). There is an internal command to list all code loaded, and entered during a session, then use the "/list" command.

/list
/list [ID]
/list [Code_Name]
/list -start
/list -all
  • /list: This command displays all of the active code entered in the current session (excluding the code loaded when JShell starts).
  • /list [ID]: This command displays the source code corresponding to the ID entered.
  • /list [Code_Name]: displays the source code corresponding to the name entered.
  • /list -start: This command displays all the code loaded when JShell starts. When starting a session, all the imports preloaded by default will be displayed, and possibly the code that you yourself wanted to preload (this point will be explained in the third part, in the chapter Customizing the start of JShell).
  • /list -all: This command lists all the active, inactive, in error and preloaded code entered during a JShell session.

In the below code snippet, we can list out all the entered imports, expressions, etc using "/list -all" command.

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.*
jshell> LocalDate localDate = LocalDate.of(2020, 04, 10)
|   Error:
|   cannot find symbol
|     symbol: class LocalDate
|   LocalDate localDate = LocalDate.of(2020, 04, 10);
|   ^-------^
|   Error:
|   cannot find symbol
|     symbol: variable LocalDate
|   LocalDate localDate = LocalDate.of(2020, 04, 10);
|   ^-------^

jshell> int x = 10
x ==> 10

jshell> public enum Operate {
...>       ADDITION,
...>       DIVISION;
...>    }
| created enum Operate

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.*;
e1 : LocalDate localDate = LocalDate.of(2020, 04, 10);
1 : int x = 10;
2 : public enum Operate {
       ADDITION,
       DIVISION;
    }

In the below code snippet, we able to name a variable, a method, or even a type with the same name. Then, we can use the "/list" command that displays different occurrences of this name with the corresponding type.

jshell> int x = 10
x ==> 10

jshell> public int x(int y) {
...>       return y;
...>    }
| created method x(int)

jshell> public long x(long y, long z) {
...>       return y + z;
...>    }
| created method x(long,long)

jshell> /list x

1 : int x = 10;
2 : public int x(int y) {
       return y;
    }
3 : public long x(long y, long z) {
       return y + z;
    }

Updated on: 09-Apr-2020

264 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements