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:

    Example)
    drag_handle
     
    jshell> int x=5
    x ==> 5
    
    jshell> public void show(){
       ...> System.out.println(x);
       ...> }
    |  created method show()
    
    jshell> /vars
    |    int x = 5
    
    jshell> /methods
    |    void show()
    
    jshell> /list
    
       1 : int x=5;
       2 : public void show(){
           System.out.println(x);
           }
    jshell>
    
    
  • Important Point

    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

    Example)
    drag_handle
     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 : int x=5;
       2 : public void show(){
           System.out.println(x);
           }
    
    jshell>

    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.

    
    jshell> /exit
    |  Goodbye
    

    Output:
    
                                                        
  • 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:

    Example)
    drag_handle
     jshell> /
    /!          /?          /drop       /edit       /env
    /exit       /help       /history    /imports    /list
    /methods    /open       /reload     /reset      /save
    /set        /types      /vars
    
    <press tab again to see synopsis>
    
    jshell> /

What next?

The next topic is Editing input

Share this page