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