How to reset the JShell session in Java 9?

Java 9 has introduced JShell for Java, and it allows us to evaluate code snippets such as declarations, statements, and expressions.

During the JShell session, we need to reset it without closing and re-opening JShell then we can use the internal command: "/reset". By using this command, code entered during the current session has erased. It can be useful when we want to test new classes, create new variables, etc. while keeping the names previously used.

In the below snippet, we have created variables x, y, and str. We can able to see all entered code snippets using the "/list" command. After that, we can apply the "/reset" command to reset the current session.

jshell> int a = 25
a ==> 25

jshell> double y = 30
y ==> 30.0

jshell> String str = "Tutorialspoint"
str ==> "Tutorialspoint"

jshell> /list

1 : int a = 25;
2 : double y = 30;
3 : String str = "Tutorialspoint";

jshell> /reset
| Resetting state.

jshell> /list

jshell> x
|  Error:
|  cannot find symbol
|    symbol: variable x
|  x
|  ^

jshell> str
|  Error:
|  cannot find symbol
|   symbol: variable str
|  str
|  ^-^

jshell> int x = 15
x ==> 15

jshell> String str = "reset"
str ==> "reset"

jshell> /list

  1 : int x = 15;
  2 : String str = "reset";

Updated on: 07-Apr-2020


Kickstart Your Career

Get certified by completing the course

Get Started