JShell commands are special commands used to control and manage a JShell session. They start with a forward slash (/) and provide features such as displaying variables, importing classes, viewing history, and exiting the session.
- JShell commands control the interactive environment rather than execute regular Java code.
- Commands such as /vars, /methods, /list, and /history help inspect the current JShell session.
- Commands such as /reset, /reload, and /exit help manage or terminate the session.
List Of JShell Commands
JShell provides several commands to manage the interactive session, inspect variables and methods, work with source files, and control the session.
1. /help Command
The /help command provides information about the commands available in JShell. It also shows the usage and description of individual JShell commands.
Example
jshell> /help
The command displays the available JShell commands along with their descriptions.
2. /list Command
The /list command displays the source code entered during the current JShell session. It also shows the corresponding snippet numbers for the entered code.
Example
jshell> int a = 10;
a ==> 10jshell> int b = 20;
b ==> 20jshell> /list
1 : int a = 10;
2 : int b = 20;
Here, /list displays the statements entered in the current session.
3. /vars Command
The /vars command displays the variables defined in the current JShell session. It shows the variable type, name, and current value.
Example
jshell> int a = 10;
a ==> 10
jshell> String name = "Java";
name ==> "Java"jshell> /vars
| int a = 10
| String name = "Java"
Here, /vars displays the variables created in the session along with their types and values.
4. /methods Command
The /methods command displays the methods defined in the current JShell session.
It shows the method name along with its parameter types.
Example
jshell> int square(int n) {
...> return n * n;
...> }
| created method square(int)jshell> /methods
| square (int)
Here, /methods displays the square() method defined in the session.
5. /types Command
The /types command displays the types defined in the current JShell session. It includes classes, interfaces, enums, and other user-defined types.
Example
jshell> class Student {
...> String name;
...> }
| created class Studentjshell> /types
| class Student
Here, /types displays the Student class created in JShell.
6. /imports Command
The /imports command displays the imports available in the current JShell session. It helps identify the packages and classes available without explicitly importing them.
Example
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.*
The exact output depends on the JDK version and the imports defined in the JShell session.
7. /history Command
The /history command displays the input entered during the current JShell session. It includes both Java code and JShell commands entered by the user.
Example
jshell> int a = 10;
a ==> 10jshell> int b = 20;
b ==> 20jshell> /history
int a = 10;
int b = 20;
/history
Here, /history displays the previously entered input.
8. /reset Command
The /reset command resets the current JShell session to its initial state. It removes variables, methods, classes, and other definitions created during the session.
Example
jshell> int a = 10;
a ==> 10jshell> /reset
| Resetting state.jshell> /vars
After /reset, the variable a is no longer defined in the session.
9. /reload Command
The /reload command reloads the previous JShell session state. It restores definitions from the previous state when the session is reloaded.
Example
jshell> int a = 10;
a ==> 10jshell> /reload
Here, /reload reloads the previous JShell state.
10. /save Command
The /save command saves the contents of a JShell session to a file. It allows the entered Java code and commands to be stored for later use.
Example
jshell> int a = 10;
a ==> 10jshell> int b = 20;
b ==> 20jshell> /save session.jsh
Here, the session contents are saved in the session.jsh file.
11. /open Command
The /open command reads and executes the contents of a specified file in JShell. It is useful for loading previously saved JShell code into a session.
Example
jshell> /open session.jsh
Here, JShell reads the contents of session.jsh and executes the code.
12. /exit Command
The /exit command terminates the current JShell session. It closes JShell and returns control to the Command Prompt or terminal.
Example
jshell> /exit
| Goodbye
Here, /exit closes the current JShell session.