1JSHELL(1) JDK Commands JSHELL(1)
2
3
4
6 jshell - interactively evaluate declarations, statements, and expres‐
7 sions of the Java programming language in a read-eval-print loop (REPL)
8
10 jshell [options] [load-files]
11
12 options
13 Command-line options, separated by spaces. See Options for
14 jshell.
15
16 load-files
17 One or more scripts to run when the tool is started. Scripts
18 can contain any valid code snippets or JShell commands.
19
20 The script can be a local file or one of following predefined
21 scripts:
22
23 DEFAULT
24 Loads the default entries, which are commonly used as im‐
25 ports.
26
27 JAVASE Imports all Java SE packages.
28
29 PRINTING
30 Defines print, println, and printf as jshell methods for
31 use within the tool.
32
33 For more than one script, use a space to separate the names.
34 Scripts are run in the order in which they're entered on the
35 command line. Command-line scripts are run after startup
36 scripts. To run a script after JShell is started, use the /open
37 command.
38
39 To accept input from standard input and suppress the interactive
40 I/O, enter a hyphen (-) for load-files. This option enables the
41 use of the jshell tool in pipe chains.
42
44 JShell provides a way to interactively evaluate declarations, state‐
45 ments, and expressions of the Java programming language, making it eas‐
46 ier to learn the language, explore unfamiliar code and APIs, and proto‐
47 type complex code. Java statements, variable definitions, method defi‐
48 nitions, class definitions, import statements, and expressions are ac‐
49 cepted. The bits of code entered are called snippets.
50
51 As snippets are entered, they're evaluated, and feedback is provided.
52 Feedback varies from the results and explanations of actions to noth‐
53 ing, depending on the snippet entered and the feedback mode chosen.
54 Errors are described regardless of the feedback mode. Start with the
55 verbose mode to get the most feedback while learning the tool.
56
57 Command-line options are available for configuring the initial environ‐
58 ment when JShell is started. Within JShell, commands are available for
59 modifying the environment as needed.
60
61 Existing snippets can be loaded from a file to initialize a JShell ses‐
62 sion, or at any time within a session. Snippets can be modified within
63 the session to try out different variations and make corrections. To
64 keep snippets for later use, save them to a file.
65
67 --add-exports module/package
68 Specifies a package to be considered as exported from its defin‐
69 ing module.
70
71 --add-modules module[,module...]
72 Specifies the root modules to resolve in addition to the initial
73 module.
74
75 -Cflag Provides a flag to pass to the compiler. To pass more than one
76 flag, provide an instance of this option for each flag or flag
77 argument needed.
78
79 --class-path path
80 Specifies the directories and archives that are searched to lo‐
81 cate class files. This option overrides the path in the CLASS‐
82 PATH environment variable. If the environment variable isn't
83 set and this option isn't used, then the current directory is
84 searched. For Linux and macOS, use a colon (:) to separate
85 items in the path. For Windows, use a semicolon (;) to separate
86 items.
87
88 --enable-preview
89 Allows code to depend on the preview features of this release.
90
91 --execution specification
92 Specifies an alternate execution engine, where specification is
93 an ExecutionControl spec. See the documentation of the package
94 jdk.jshell.spi for the syntax of the spec.
95
96 --feedback mode
97 Sets the initial level of feedback provided in response to
98 what's entered. The initial level can be overridden within a
99 session by using the /set feedback mode command. The default is
100 normal.
101
102 The following values are valid for mode:
103
104 verbose
105 Provides detailed feedback for entries. Additional in‐
106 formation about the action performed is displayed after
107 the result of the action. The next prompt is separated
108 from the feedback by a blank line.
109
110 normal Provides an average amount of feedback. The next prompt
111 is separated from the feedback by a blank line.
112
113 concise
114 Provides minimal feedback. The next prompt immediately
115 follows the code snippet or feedback.
116
117 silent Provides no feedback. The next prompt immediately fol‐
118 lows the code snippet.
119
120 custom Provides custom feedback based on how the mode is de‐
121 fined. Custom feedback modes are created within JShell
122 by using the /set mode command.
123
124 --help or -h or -?
125 Prints a summary of standard options and exits the tool.
126
127 --help-extra or -X
128 Prints a summary of nonstandard options and exits the tool.
129 Nonstandard options are subject to change without notice.
130
131 -Jflag Provides a flag to pass to the runtime system. To pass more
132 than one flag, provide an instance of this option for each flag
133 or flag argument needed.
134
135 --module-path modulepath
136 Specifies where to find application modules. For Linux and mac‐
137 OS, use a colon (:) to separate items in the path. For Windows,
138 use a semicolon (;) to separate items.
139
140 --no-startup
141 Prevents startup scripts from running when JShell starts. Use
142 this option to run only the scripts entered on the command line
143 when JShell is started, or to start JShell without any preloaded
144 information if no scripts are entered. This option can't be
145 used if the --startup option is used.
146
147 -q Sets the feedback mode to concise, which is the same as entering
148 --feedback concise.
149
150 -Rflag Provides a flag to pass to the remote runtime system. To pass
151 more than one flag, provide an instance of this option for each
152 flag or flag argument to pass.
153
154 -s Sets the feedback mode to silent, which is the same as entering
155 --feedback silent.
156
157 --show-version
158 Prints version information and enters the tool.
159
160 --startup file
161 Overrides the default startup script for this session. The
162 script can contain any valid code snippets or commands.
163
164 The script can be a local file or one of the following prede‐
165 fined scripts:
166
167 DEFAULT
168 Loads the default entries, which are commonly used as im‐
169 ports.
170
171 JAVASE Imports all Java SE packages.
172
173 PRINTING
174 Defines print, println, and printf as jshell methods for
175 use within the tool.
176
177 For more than one script, provide a separate instance of this
178 option for each script. Startup scripts are run when JShell is
179 first started and when the session is restarted with the /reset,
180 /reload, or /env command. Startup scripts are run in the order
181 in which they're entered on the command line.
182
183 This option can't be used if the --no-startup option is used.
184
185 -v Sets the feedback mode to verbose, which is the same as entering
186 --feedback verbose.
187
188 --version
189 Prints version information and exits the tool.
190
192 Within the jshell tool, commands are used to modify the environment and
193 manage code snippets.
194
195 /drop {name|id|startID-endID} [{name|id|startID-endID}...]
196 Drops snippets identified by name, ID, or ID range, making them
197 inactive. For a range of IDs, provide the starting ID and end‐
198 ing ID separated with a hyphen. To provide a list, separate the
199 items in the list with a space. Use the /list command to see
200 the IDs of code snippets.
201
202 /edit [option]
203 Opens an editor. If no option is entered, then the editor opens
204 with the active snippets.
205
206 The following options are valid:
207
208 {name|id|startID-endID} [{name|id|startID-endID}...]
209 Opens the editor with the snippets identified by name,
210 ID, or ID range. For a range of IDs, provide the start‐
211 ing ID and ending ID separated with a hyphen. To provide
212 a list, separate the items in the list with a space. Use
213 the /list command to see the IDs of code snippets.
214
215 -all Opens the editor with all snippets, including startup
216 snippets and snippets that failed, were overwritten, or
217 were dropped.
218
219 -start Opens the editor with startup snippets that were evaluat‐
220 ed when JShell was started.
221
222 To exit edit mode, close the editor window, or respond to the
223 prompt provided if the -wait option was used when the editor was
224 set.
225
226 Use the /set editor command to specify the editor to use. If no
227 editor is set, then the following environment variables are
228 checked in order: JSHELLEDITOR, VISUAL, and EDITOR. If no edi‐
229 tor is set in JShell and none of the editor environment vari‐
230 ables is set, then a simple default editor is used.
231
232 /env [options]
233 Displays the environment settings, or updates the environment
234 settings and restarts the session. If no option is entered,
235 then the current environment settings are displayed. If one or
236 more options are entered, then the session is restarted as fol‐
237 lows:
238
239 • Updates the environment settings with the provided options.
240
241 • Resets the execution state.
242
243 • Runs the startup scripts.
244
245 • Silently replays the history in the order entered. The histo‐
246 ry includes all valid snippets or /drop commands entered at
247 the jshell prompt, in scripts entered on the command line, or
248 scripts entered with the /open command.
249
250 Environment settings entered on the command line or provided
251 with a previous /reset, /env, or /reload command are maintained
252 unless an option is entered that overwrites the setting.
253
254 The following options are valid:
255
256 --add-modules module[,module...]
257 Specifies the root modules to resolve in addition to the
258 initial module.
259
260 --add-exports source-module/package=target-module[,target-mod‐
261 ule]*
262 Adds an export of package from source-module to tar‐
263 get-module.
264
265 --class-path path
266 Specifies the directories and archives that are searched
267 to locate class files. This option overrides the path in
268 the CLASSPATH environment variable. If the environment
269 variable isn't set and this option isn't used, then the
270 current directory is searched. For Linux and macOS, use
271 a colon (:) to separate items in the path. For Windows,
272 use a semicolon (;) to separate items.
273
274 --module-path modulepath
275 Specifies where to find application modules. For Linux
276 and macOS, use a colon (:) to separate items in the path.
277 For Windows, use a semicolon (;) to separate items.
278
279 /exit [integer-expression-snippet]
280 Exits the tool. If no snippet is entered, the exit status is
281 zero. If a snippet is entered and the result of the snippet is
282 an integer, the result is used as the exit status. If an error
283 occurs, or the result of the snippet is not an integer, an error
284 is displayed and the tool remains active.
285
286 /history
287 Displays what was entered in this session.
288
289 /help [command|subject]
290 Displays information about commands and subjects. If no options
291 are entered, then a summary of information for all commands and
292 a list of available subjects are displayed. If a valid command
293 is provided, then expanded information for that command is dis‐
294 played. If a valid subject is entered, then information about
295 that subject is displayed.
296
297 The following values for subject are valid:
298
299 context
300 Describes the options that are available for configuring
301 the environment.
302
303 intro Provides an introduction to the tool.
304
305 shortcuts
306 Describes keystrokes for completing commands and snip‐
307 pets. See Input Shortcuts.
308
309 /imports
310 Displays the current active imports, including those from the
311 startup scripts and scripts that were entered on the command
312 line when JShell was started.
313
314 /list [option]
315 Displays a list of snippets and their IDs. If no option is en‐
316 tered, then all active snippets are displayed, but startup snip‐
317 pets aren't.
318
319 The following options are valid:
320
321 {name|id|startID-endID} [{name|id|startID-endID}...]
322 Displays the snippets identified by name, ID, or ID
323 range. For a range of IDs, provide the starting ID and
324 ending ID separated with a hyphen. To provide a list,
325 separate the items in the list with a space.
326
327 -all Displays all snippets, including startup snippets and
328 snippets that failed, were overwritten, or were dropped.
329 IDs that begin with s are startup snippets. IDs that be‐
330 gin with e are snippets that failed.
331
332 -start Displays startup snippets that were evaluated when JShell
333 was started.
334
335 /methods [option]
336 Displays information about the methods that were entered. If no
337 option is entered, then the name, parameter types, and return
338 type of all active methods are displayed.
339
340 The following options are valid:
341
342 {name|id|startID-endID} [{name|id|startID-endID}...]
343 Displays information for methods identified by name, ID,
344 or ID range. For a range of IDs, provide the starting ID
345 and ending ID separated with a hyphen. To provide a
346 list, separate the items in the list with a space. Use
347 the /list command to see the IDs of code snippets.
348
349 -all Displays information for all methods, including those
350 added when JShell was started, and methods that failed,
351 were overwritten, or were dropped.
352
353 -start Displays information for startup methods that were added
354 when JShell was started.
355
356 /open file
357 Opens the script specified and reads the snippets into the tool.
358 The script can be a local file or one of the following prede‐
359 fined scripts:
360
361 DEFAULT
362 Loads the default entries, which are commonly used as im‐
363 ports.
364
365 JAVASE Imports all Java SE packages.
366
367 PRINTING
368 Defines print, println, and printf as jshell methods for
369 use within the tool.
370
371 /reload [options]
372 Restarts the session as follows:
373
374 • Updates the environment settings with the provided options, if
375 any.
376
377 • Resets the execution state.
378
379 • Runs the startup scripts.
380
381 • Replays the history in the order entered. The history in‐
382 cludes all valid snippets or /drop commands entered at the
383 jshell prompt, in scripts entered on the command line, or
384 scripts entered with the /open command.
385
386 Environment settings entered on the command line or provided
387 with a previous /reset, /env, or /reload command are maintained
388 unless an option is entered that overwrites the setting.
389
390 The following options are valid:
391
392 --add-modules module[,module...]
393 Specifies the root modules to resolve in addition to the
394 initial module.
395
396 --add-exports source-module/package=target-module[,target-mod‐
397 ule]*
398 Adds an export of package from source-module to tar‐
399 get-module.
400
401 --class-path path
402 Specifies the directories and archives that are searched
403 to locate class files. This option overrides the path in
404 the CLASSPATH environment variable. If the environment
405 variable isn't set and this option isn't used, then the
406 current directory is searched. For Linux and macOS, use
407 a colon (:) to separate items in the path. For Windows,
408 use a semicolon (;) to separate items.
409
410 --module-path modulepath
411 Specifies where to find application modules. For Linux
412 and macOS, use a colon (:) to separate items in the path.
413 For Windows, use a semicolon (;) to separate items.
414
415 -quiet Replays the valid history without displaying it. Errors
416 are displayed.
417
418 -restore
419 Resets the environment to the state at the start of the
420 previous run of the tool or to the last time a /reset,
421 /reload, or /env command was executed in the previous
422 run. The valid history since that point is replayed.
423 Use this option to restore a previous JShell session.
424
425 /reset [options]
426 Discards all entered snippets and restarts the session as fol‐
427 lows:
428
429 • Updates the environment settings with the provided options, if
430 any.
431
432 • Resets the execution state.
433
434 • Runs the startup scripts.
435
436 History is not replayed. All code that was entered is lost.
437
438 Environment settings entered on the command line or provided
439 with a previous /reset, /env, or /reload command are maintained
440 unless an option is entered that overwrites the setting.
441
442 The following options are valid:
443
444 --add-modules module[,module...]
445 Specifies the root modules to resolve in addition to the
446 initial module.
447
448 --add-exports source-module/package=target-module[,target-mod‐
449 ule]*
450 Adds an export of package from source-module to tar‐
451 get-module.
452
453 --class-path path
454 Specifies the directories and archives that are searched
455 to locate class files. This option overrides the path in
456 the CLASSPATH environment variable. If the environment
457 variable isn't set and this option isn't used, then the
458 current directory is searched. For Linux and macOS, use
459 a colon (:) to separate items in the path. For Windows,
460 use a semicolon (;) to separate items.
461
462 --module-path modulepath
463 Specifies where to find application modules. For Linux
464 and macOS, use a colon (:) to separate items in the path.
465 For Windows, use a semicolon (;) to separate items.
466
467 /save [options] file
468 Saves snippets and commands to the file specified. If no op‐
469 tions are entered, then active snippets are saved.
470
471 The following options are valid:
472
473 {name|id|startID-endID} [{name|id|startID-endID}...]
474 Saves the snippets and commands identified by name, ID,
475 or ID range. For a range of IDs, provide the starting ID
476 and ending ID separated with a hyphen. To provide a
477 list, separate the items in the list with a space. Use
478 the /list command to see the IDs of the code snippets.
479
480 -all Saves all snippets, including startup snippets and snip‐
481 pets that were overwritten or failed.
482
483 -history
484 Saves the sequential history of all commands and snippets
485 entered in the current session.
486
487 -start Saves the current startup settings. If no startup
488 scripts were provided, then an empty file is saved.
489
490 /set [setting]
491 Sets configuration information, including the external editor,
492 startup settings, and feedback mode. This command is also used
493 to create a custom feedback mode with customized prompt, format,
494 and truncation values. If no setting is entered, then the cur‐
495 rent setting for the editor, startup settings, and feedback mode
496 are displayed.
497
498 The following values are valid for setting:
499
500 editor [options] [command]
501 Sets the command used to start an external editor when
502 the /edit command is entered. The command can include
503 command arguments separated by spaces. If no command or
504 options are entered, then the current setting is dis‐
505 played.
506
507 The following options are valid:
508
509 -default
510 Sets the editor to the default editor provided
511 with JShell. This option can't be used if a com‐
512 mand for starting an editor is entered.
513
514 -delete
515 Sets the editor to the one in effect when the ses‐
516 sion started. If used with the -retain option,
517 then the retained editor setting is deleted and
518 the editor is set to the first of the following
519 environment variables found: JSHELLEDITOR, VISUAL,
520 or EDITOR. If none of the editor environment
521 variables are set, then this option sets the edi‐
522 tor to the default editor.
523
524 This option can't be used if a command for start‐
525 ing an editor is entered.
526
527 -retain
528 Saves the editor setting across sessions. If no
529 other option or a command is entered, then the
530 current setting is saved.
531
532 -wait Prompts the user to indicate when editing is com‐
533 plete. Otherwise control returns to JShell when
534 the editor exits. Use this option if the editor
535 being used exits immediately, for example, when an
536 edit window already exists. This option is valid
537 only when a command for starting an editor is en‐
538 tered.
539
540 feedback [mode]
541 Sets the feedback mode used to respond to input. If no
542 mode is entered, then the current mode is displayed.
543
544 The following modes are valid: concise, normal, silent,
545 verbose, and any custom mode created with the /set mode
546 command.
547
548 format mode field "format-string" selector
549 Sets the format of the feedback provided in response to
550 input. If no mode is entered, then the current formats
551 for all fields for all feedback modes are displayed. If
552 only a mode is entered, then the current formats for that
553 mode are displayed. If only a mode and field are en‐
554 tered, then the current formats for that field are dis‐
555 played.
556
557 To define a format, the following arguments are required:
558
559 mode Specifies a feedback mode to which the response
560 format is applied. Only custom modes created with
561 the /set mode command can be modified.
562
563 field Specifies a context-specific field to which the
564 response format is applied. The fields are de‐
565 scribed in the online help, which is accessed from
566 JShell using the /help /set format command.
567
568 "format-string"
569 Specifies the string to use as the response format
570 for the specified field and selector. The struc‐
571 ture of the format string is described in the on‐
572 line help, which is accessed from JShell using the
573 /help /set format command.
574
575 selector
576 Specifies the context in which the response format
577 is applied. The selectors are described in the
578 online help, which is accessed from JShell using
579 the /help /set format command.
580
581 mode [mode-name] [existing-mode] [options]
582 Creates a custom feedback mode with the mode name provid‐
583 ed. If no mode name is entered, then the settings for
584 all modes are displayed, which includes the mode, prompt,
585 format, and truncation settings. If the name of an ex‐
586 isting mode is provided, then the settings from the ex‐
587 isting mode are copied to the mode being created.
588
589 The following options are valid:
590
591 -command|-quiet
592 Specifies the level of feedback displayed for com‐
593 mands when using the mode. This option is re‐
594 quired when creating a feedback mode. Use -com‐
595 mand to show information and verification feedback
596 for commands. Use -quiet to show only essential
597 feedback for commands, such as error messages.
598
599 -delete
600 Deletes the named feedback mode for this session.
601 The name of the mode to delete is required. To
602 permanently delete a retained mode, use the -re‐
603 tain option with this option. Predefined modes
604 can't be deleted.
605
606 -retain
607 Saves the named feedback mode across sessions.
608 The name of the mode to retain is required.
609
610 Configure the new feedback mode using the /set prompt,
611 /set format, and /set truncation commands.
612
613 To start using the new mode, use the /set feedback com‐
614 mand.
615
616 prompt mode "prompt-string" "continuation-prompt-string"
617 Sets the prompts for input within JShell. If no mode is
618 entered, then the current prompts for all feedback modes
619 are displayed. If only a mode is entered, then the cur‐
620 rent prompts for that mode are displayed.
621
622 To define a prompt, the following arguments are required:
623
624 mode Specifies the feedback mode to which the prompts
625 are applied. Only custom modes created with the
626 /set mode command can be modified.
627
628 "prompt-string"
629 Specifies the string to use as the prompt for the
630 first line of input.
631
632 "continuation-prompt-string"
633 Specifies the string to use as the prompt for the
634 additional input lines needed to complete a snip‐
635 pet.
636
637 start [-retain] [file [file...]|option]
638 Sets the names of the startup scripts used when the next
639 /reset, /reload, or /env command is entered. If more
640 than one script is entered, then the scripts are run in
641 the order entered. If no scripts or options are entered,
642 then the current startup settings are displayed.
643
644 The scripts can be local files or one of the following
645 predefined scripts:
646
647 DEFAULT
648 Loads the default entries, which are commonly used
649 as imports.
650
651 JAVASE Imports all Java SE packages.
652
653 PRINTING
654 Defines print, println, and printf as jshell meth‐
655 ods for use within the tool.
656
657 The following options are valid:
658
659 -default
660 Sets the startup settings to the default settings.
661
662 -none Specifies that no startup settings are used.
663
664 Use the -retain option to save the start setting across
665 sessions.
666
667 truncation mode length selector
668 Sets the maximum length of a displayed value. If no mode
669 is entered, then the current truncation values for all
670 feedback modes are displayed. If only a mode is entered,
671 then the current truncation values for that mode are dis‐
672 played.
673
674 To define truncation values, the following arguments are
675 required:
676
677 mode Specifies the feedback mode to which the trunca‐
678 tion value is applied. Only custom modes created
679 with the /set mode command can be modified.
680
681 length Specifies the unsigned integer to use as the maxi‐
682 mum length for the specified selector.
683
684 selector
685 Specifies the context in which the truncation val‐
686 ue is applied. The selectors are described in the
687 online help, which is accessed from JShell using
688 the /help /set truncation command.
689
690 /types [option]
691 Displays classes, interfaces, and enums that were entered. If
692 no option is entered, then all current active classes, inter‐
693 faces, and enums are displayed.
694
695 The following options are valid:
696
697 {name|id|startID-endID} [{name|id|startID-endID}...]
698 Displays information for classes, interfaces, and enums
699 identified by name, ID, or ID range. For a range of IDs,
700 provide the starting ID and ending ID separated with a
701 hyphen. To provide a list, separate the items in the
702 list with a space. Use the /list command to see the IDs
703 of the code snippets.
704
705 -all Displays information for all classes, interfaces, and
706 enums, including those added when JShell was started, and
707 classes, interfaces, and enums that failed, were over‐
708 written, or were dropped.
709
710 -start Displays information for startup classes, interfaces, and
711 enums that were added when JShell was started.
712
713 /vars [option]
714 Displays the name, type, and value of variables that were en‐
715 tered. If no option is entered, then all current active vari‐
716 ables are displayed.
717
718 The following options are valid:
719
720 {name|id|startID-endID} [{name|id|startID-endID}...]
721 Displays information for variables identified by name,
722 ID, or ID range. For a range of IDs, provide the start‐
723 ing ID and ending ID separated with a hyphen. To provide
724 a list, separate the items in the list with a space. Use
725 the /list command to see the IDs of the code snippets.
726
727 -all Displays information for all variables, including those
728 added when JShell was started, and variables that failed,
729 were overwritten, or were dropped.
730
731 -start Displays information for startup variables that were
732 added when JShell was started.
733
734 /? Same as the /help command.
735
736 /! Reruns the last snippet.
737
738 /{name|id|startID-endID} [{name|id|startID-endID}...]
739 Reruns the snippets identified by ID, range of IDs, or name.
740 For a range of IDs, provide the starting ID and ending ID sepa‐
741 rated with a hyphen. To provide a list, separate the items in
742 the list with a space. The first item in the list must be an ID
743 or ID range. Use the /list command to see the IDs of the code
744 snippets.
745
746 /-n Reruns the -nth previous snippet. For example, if 15 code snip‐
747 pets were entered, then /-4 runs the 11th snippet. Commands
748 aren't included in the count.
749
751 The following shortcuts are available for entering commands and snip‐
752 pets in JShell.
753
754 Tab completion
755 <tab> When entering snippets, commands, subcommands, command argu‐
756 ments, or command options, use the Tab key to automatically com‐
757 plete the item. If the item can't be determined from what was
758 entered, then possible options are provided.
759
760 When entering a method call, use the Tab key after the method
761 call's opening parenthesis to see the parameters for the method.
762 If the method has more than one signature, then all signatures
763 are displayed. Pressing the Tab key a second time displays the
764 description of the method and the parameters for the first sig‐
765 nature. Continue pressing the Tab key for a description of any
766 additional signatures.
767
768 Shift+<Tab> V
769 After entering a complete expression, use this key sequence to
770 convert the expression to a variable declaration of a type de‐
771 termined by the type of the expression.
772
773 Shift+<Tab> M
774 After entering a complete expression or statement, use this key
775 sequence to convert the expression or statement to a method dec‐
776 laration. If an expression is entered, the return type is based
777 on the type of the expression.
778
779 Shift+<Tab> I
780 When an identifier is entered that can't be resolved, use this
781 key sequence to show possible imports that resolve the identifi‐
782 er based on the content of the specified class path.
783
784 Command abbreviations
785 An abbreviation of a command is accepted if the abbreviation uniquely
786 identifies a command. For example, /l is recognized as the /list com‐
787 mand. However, /s isn't a valid abbreviation because it can't be de‐
788 termined if the /set or /save command is meant. Use /se for the /set
789 command or /sa for the /save command.
790
791 Abbreviations are also accepted for subcommands, command arguments, and
792 command options. For example, use /m -a to display all methods.
793
794 History navigation
795 A history of what was entered is maintained across sessions. Use the
796 up and down arrows to scroll through commands and snippets from the
797 current and past sessions. Use the Ctrl key with the up and down ar‐
798 rows to skip all but the first line of multiline snippets.
799
800 History search
801 Use the Ctrl+R key combination to search the history for the string en‐
802 tered. The prompt changes to show the string and the match. Ctrl+R
803 searches backwards from the current location in the history through
804 earlier entries. Ctrl+S searches forward from the current location in
805 the history though later entries.
806
808 The editing capabilities of JShell are similar to that of other common
809 shells. Keyboard keys and key combinations provide line editing short‐
810 cuts. The Ctrl key and Meta key are used in key combinations. If your
811 keyboard doesn't have a Meta key, then the Alt key is often mapped to
812 provide Meta key functionality.
813
814 Key or Key Combination Action
815 ────────────────────────────────────────────────────
816 Return Enter the current line.
817 Left arrow Move the cursor to the
818 left one character.
819 Right arrow Move the cursor to the
820 right one character.
821 Ctrl+A Move the cursor to the be‐
822 ginning of the line.
823 Ctrl+E Move the cursor to the end
824 of the line.
825 Meta+B Move the cursor to the
826 left one word.
827 Meta+F Move the cursor to the
828 right one word.
829 Delete Delete the character under
830 the cursor.
831 Backspace Delete the character be‐
832 fore the cursor.
833 Ctrl+K Delete the text from the
834 cursor to the end of the
835 line.
836 Meta+D Delete the text from the
837 cursor to the end of the
838 word.
839 Ctrl+W Delete the text from the
840 cursor to the previous
841 white space.
842
843
844 Ctrl+Y Paste the most recently
845 deleted text into the
846 line.
847 Meta+Y After Ctrl+Y, press to cy‐
848 cle through the previously
849 deleted text.
850
852 JShell is provided with the JDK. To start a session, enter jshell on
853 the command line. A welcome message is printed, and a prompt for en‐
854 tering commands and snippets is provided.
855
856 % jshell
857 | Welcome to JShell -- Version 9
858 | For an introduction type: /help intro
859
860 jshell>
861
862 To see which snippets were automatically loaded when JShell started,
863 use the /list -start command. The default startup snippets are import
864 statements for common packages. The ID for each snippet begins with
865 the letter s, which indicates it's a startup snippet.
866
867 jshell> /list -start
868
869 s1 : import java.io.*;
870 s2 : import java.math.*;
871 s3 : import java.net.*;
872 s4 : import java.nio.file.*;
873 s5 : import java.util.*;
874 s6 : import java.util.concurrent.*;
875 s7 : import java.util.function.*;
876 s8 : import java.util.prefs.*;
877 s9 : import java.util.regex.*;
878 s10 : import java.util.stream.*;
879
880 jshell>
881
882 To end the session, use the /exit command.
883
884 jshell> /exit
885 | Goodbye
886
887 %
888
890 Snippets are Java statements, variable definitions, method definitions,
891 class definitions, import statements, and expressions. Terminating
892 semicolons are automatically added to the end of a completed snippet if
893 they're missing.
894
895 The following example shows two variables and a method being defined,
896 and the method being run. Note that a scratch variable is automatical‐
897 ly created to hold the result because no variable was provided.
898
899 jshell> int a=4
900 a ==> 4
901
902 jshell> int b=8
903 b ==> 8
904
905 jshell> int square(int i1) {
906 ...> return i1 * i1;
907 ...> }
908 | created method square(int)
909
910 jshell> square(b)
911 $5 ==> 64
912
914 Change the definition of a variable, method, or class by entering it
915 again.
916
917 The following examples shows a method being defined and the method run:
918
919 jshell> String grade(int testScore) {
920 ...> if (testScore >= 90) {
921 ...> return "Pass";
922 ...> }
923 ...> return "Fail";
924 ...> }
925 | created method grade(int)
926
927 jshell> grade(88)
928 $3 ==> "Fail"
929
930 To change the method grade to allow more students to pass, enter the
931 method definition again and change the pass score to 80. Use the up
932 arrow key to retrieve the previous entries to avoid having to reenter
933 them and make the change in the if statement. The following example
934 shows the new definition and reruns the method to show the new result:
935
936 jshell> String grade(int testScore) {
937 ...> if (testScore >= 80) {
938 ...> return "Pass";
939 ...> }
940 ...> return "Fail";
941 ...> }
942 | modified method grade(int)
943
944 jshell> grade(88)
945 $5 ==> "Pass"
946
947 For snippets that are more than a few lines long, or to make more than
948 a few changes, use the /edit command to open the snippet in an editor.
949 After the changes are complete, close the edit window to return control
950 to the JShell session. The following example shows the command and the
951 feedback provided when the edit window is closed. The /list command is
952 used to show that the pass score was changed to 85.
953
954 jshell> /edit grade
955 | modified method grade(int)
956 jshell> /list grade
957
958 6 : String grade(int testScore) {
959 if (testScore >= 85) {
960 return "Pass";
961 }
962 return "Fail";
963 }
964
966 The feedback mode determines the prompt that's displayed, the feedback
967 messages that are provided as snippets are entered, and the maximum
968 length of a displayed value. Predefined feedback modes are provided.
969 Commands for creating custom feedback modes are also provided.
970
971 Use the /set mode command to create a new feedback mode. In the fol‐
972 lowing example, the new mode mymode, is based on the predefined feed‐
973 back mode, normal, and verifying command feedback is displayed:
974
975 jshell> /set mode mymode normal -command
976 | Created new feedback mode: mymode
977
978 Because the new mode is based on the normal mode, the prompts are the
979 same. The following example shows how to see what prompts are used and
980 then changes the prompts to custom strings. The first string repre‐
981 sents the standard JShell prompt. The second string represents the
982 prompt for additional lines in multiline snippets.
983
984 jshell> /set prompt mymode
985 | /set prompt mymode "\njshell> " " ...> "
986
987 jshell> /set prompt mymode "\nprompt$ " " continue$ "
988
989 The maximum length of a displayed value is controlled by the truncation
990 setting. Different types of values can have different lengths. The
991 following example sets an overall truncation value of 72, and a trunca‐
992 tion value of 500 for variable value expressions:
993
994 jshell> /set truncation mymode 72
995
996 jshell> /set truncation mymode 500 varvalue
997
998 The feedback displayed after snippets are entered is controlled by the
999 format setting and is based on the type of snippet entered and the ac‐
1000 tion taken for that snippet. In the predefined mode normal, the string
1001 created is displayed when a method is created. The following example
1002 shows how to change that string to defined:
1003
1004 jshell> /set format mymode action "defined" added-primary
1005
1006 Use the /set feedback command to start using the feedback mode that was
1007 just created. The following example shows the custom mode in use:
1008
1009 jshell> /set feedback mymode
1010 | Feedback mode: mymode
1011
1012 prompt$ int square (int num1){
1013 continue$ return num1*num1;
1014 continue$ }
1015 | defined method square(int)
1016
1017 prompt$
1018
1019
1020
1021JDK 17 2021 JSHELL(1)