Getting started with JShell

JShell is a command-line tool that is located in the /bin folder. The syntax for this tool is as follows:

jshell <options> <load files>

As you can see in the following screenshot, there are several options that can be used with this tool:

You have already seen the -h option, which we executed with jshell -h. This provided the listing of JShell options.

To log into your JShell, you can simply use the jshell command. You will see that the prompt in the command window changes accordingly:

 

Exiting the shell is as easy as entering /exit. Once inside the JShell, you can enter any of the following commands:

Command Functionality
/drop

Use this command to delete a source entry that is referenced by name or id.

Here is the syntax:

/drop <name or id>

/edit

With this command, you can edit a source entry using name or id reference. 

Here is the syntax:

/edit <name or id>

/env

This powerful command allows you to view or change the evaluation context. 

Here is the syntax:

/env [-class-path <path>]  [-module-path <path>]  [-add-modules <modules>]

/exit

This command is used to exit the JShell. The syntax is simply /exit without any options or parameters available.

/history

The history command provides a history of what you have typed. The syntax is simply /history without any options or parameters available.

/<id>

This command is used to rerun a previous snippet by referencing the id.

Here is the syntax: /<id>

You can also run a specific snippet by referencing the nth previous snippet with /-<n>.

/imports

You can use this command to list the imported items. The syntax is /imports and it does not accept any options or parameters.

/list

This command will list the source you typed.

Here is the syntax:

/list [<name or id> | -all | -start]

/methods

This command lists all declared methods as well as their signatures.

Here is the syntax:

/methods [<name or id> | -all | -start]

/open

Using this command, you can open a file as a source input.

Here is the syntax:

/open <file>

/reload

The reload command gives you the ability to reset and replay relevant history.

Here is the syntax:

/reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]

/reset

This command resets the JShell.

Here is the syntax:

/reset [-class-path <path>] [-module-path <path>] [-add-modules <modules]

/save

This command saves the snippet source to a file specified by you.

Here is the syntax:

/save [-all | -history | -start] <file>

/set

This command is used to set the JShell configuration information.

Here is the syntax:

/set editor | start | feedback | mode | prompt | truncation | format

/types

This command simply lists declared types.

Here is the syntax:

/types [<name or id> | -all | -start]

/vars

This command lists all declared variables as well as their values.

Here is the syntax:

/vars [<name or id> | -all | -start]

/!

This command will rerun the last snippet. The syntax is simply /!

Several of the previously listed commands use the term snippet. In the context of Java and JShell, a snippet is one of the following:
  • ClassDeclaration
  • Expression
  • FieldDeclaration
  • ImportDeclaration
  • InterfaceDeclaration
  • MethodDeclaration

Entering the /help or /? command in the JShell provides a complete list of commands and syntax that can be used in the shell. This list is provided as follows:

You are encouraged to experiment with JShell commands. You can use the preceding screenshot to remind yourself of the proper syntax.

The /help command can be especially helpful if you are still new to JShell. As you can see in the following screenshot, we can obtain an introduction to JShell by simply entering the /help intro command:

If you find yourself using JShell often, you might benefit from one or more of the following listed shortcuts. These can be listed at any time from within JShell by using the /help shortcuts command:

Additional help can be obtained from within the JShell by using the /help command followed by the command you want additional help on. For example, entering /help reload provides detailed information regarding the /reload command. This information is provided as follows:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset