3 Commands

JShell commands are entered in a JShell session, and used to control the environment and display information.

Introduction to Commands

JShell commands control the environment and display information within a session.

Commands are distinguished from snippets by a leading forward slash (/). For information about the current variables, methods, and types, use the /vars, /methods, and /types commands. For a list of entered snippets, use the /list command. The following example shows these commands based on the examples in Trying Out Snippets:

jshell> /vars
|    int x = 45
|    int $3 = 4
|    String $5 = "OceanOcean"

jshell> /methods
|    twice (String)String

jshell> /list

   1 : System.out.println("Hi");
   2 : int x = 45;
   3 : 2 + 2
   4 : String twice(String s) {
         return s + s;
       }
   5 : twice("Ocean")

Notice that the types and values of variables and the type signature of methods are displayed.

JShell has a default startup script that is silently and automatically executed before JShell starts, so that you can get to work quickly. Entries from the startup script aren't listed unless you request them with the /list -start or /list -all command:

jshell> /list -all

  s1 : import java.util.*;
  s2 : import java.io.*;
  s3 : import java.math.*;
  s4 : import java.net.*;
  s5 : import java.util.concurrent.*;
  s6 : import java.util.prefs.*;
  s7 : import java.util.regex.*;
   1 : System.out.println("Hi");
   2 : int x = 45;
   3 : 2 + 2
   4 : String twice(String s) {
         return s + s;
       }
   5 : twice("Ocean")

The default startup script consists of several common imports. You can personalize your startup entries with the /set start command. For information about this command, enter /help /set start. The /save -start command saves the current startup script as a starting point for your own startup script.

Other important commands include /exit to leave JShell, /save to save your snippets, and /open to enter snippets from a file.

Enter /help for a list of the JShell commands.

Tab Completion for Commands

Similar to snippet completion, when you enter commands and command options, use the Tab key to automatically complete the command or option. If the completion can’t be determined from what was entered, then possible choices are provided.

The following example shows the feedback when Tab is pressed after the leading slash (/) for commands:

jshell> /<Tab>
/!          /?          /drop       /edit       /env        /exit       /help
/history    /imports    /list       /methods    /open       /reload     /reset      
/save       /set        /types      /vars       

<press tab again to see synopsis>

jshell> /

Unique completions are done in-place. For example, after you enter /l and press Tab, the line is replaced with /list:

jshell> /l<Tab>

jshell> /list

Tab completion also works for command options. The following example shows the use of the Tab key to display the options for the /list command:

jshell> /list -<Tab>
-all       -history   -start     

<press tab again to see synopsis>

jshell> /list -

Notice the message about pressing Tab again to show the command synopsis, which is a short description of the command. Press Tab a third time to show the help documentation. The following example shows the results of pressing Tab a second and third time:

jshell> /list -<Tab>
list the source you have typed

<press tab again to see full documentation>

jshell> /list -<Tab>
Show the source of snippets, prefaced with the snippet id.

/list
    List the currently active snippets of code that you typed or read with /open

/list -start
    List the automatically evaluated start-up snippets

/list -all
    List all snippets including failed, overwritten, dropped, and start-up

/list <name>
    List snippets with the specified name (preference for active snippets)

/list <id>
    List the snippet with the specified snippet id

jshell> /list -

Completion of unique arguments is done in place. For example, after you enter /list -a<Tab>, the -all option is automatically shown:

jshell> /list -a<Tab>

jshell> /list -all

Snippet names can also be completed with Tab. For example, if you defined the volume method earlier in the JShell session, then pressing Tab after you start to enter the method name results in the full method name being displayed:

jshell> /ed v<Tab>

jshell> /ed volume

Using Tab in a file argument position of the command shows the available files:

jshell> /open <Tab>
myfile1      myfile2    definitions.jsh

<press tab again to see synopsis>

jshell> /open 

Completion of unique file names is done in place:

jshell> /open d<Tab> 

jshell> /open definitions.jsh

Command Abbreviations

Reduce the amount of typing you have to do by using abbreviations. Commands, /set subcommands, command arguments, and command options can all be abbreviated, as long as the abbreviation is unique.

The only command that begins with /l is /list, and the only /list option that begins with -a is -all . Therefore, you can use the following abbreviations to enter the /list -all command :

jshell> /l -a

Also, the only command that begins with /se is /set, the only /set subcommand that begins with fe is feedback, and the only feedback mode that begins with v is verbose, assuming no custom feedback modes that start with v exist. Therefore, you can use the following abbreviations to set the feedback mode to verbose:

jshell> /se fe v

Notice that /s isn’t a sufficient abbreviation because /save and /set both begin with the same letter. When in doubt, you can use Tab completion to see the options.