Skip to content

Instantly share code, notes, and snippets.

@jeszy75
Last active February 28, 2025 08:21
Show Gist options
  • Select an option

  • Save jeszy75/a42894e1c72abd3f6920929de5491a53 to your computer and use it in GitHub Desktop.

Select an option

Save jeszy75/a42894e1c72abd3f6920929de5491a53 to your computer and use it in GitHub Desktop.
Working with JShell

Working with JShell

What is JShell?

JShell is a tool that lets you execute Java code interactively. It is part of the JDK, starting with Java SE 9.

Commands

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

Useful commands:

Command Description
/exit Exits JShell (nothing is saved).
/help Displays help about using JShell.
/history Lists the snippets and commands entered.
/imports Lists the current active imports.
/list Lists the snippets entered.
/methods Lists the methods entered.
/reset Resets JShell (clears snippets entered).
/save <file> Saves the snippets entered.
/save <id> <file> Saves the snippet specified.
/vars Lists the variables entered.
/! Reevaluates the last snippet.
/<id> Reevaluates the snippet specified.

Editing

External Editor

Setting an external editor:

/set editor gedit

Editing all existing snippets:

/edit

When you save in the editor, any changed or new snippet is entered into the JShell session.

Editing a specific snippet:

/edit <id>

Feedback Modes

The feedback mode determines the prompts, feedback, and other interactions within JShell.

Showing the current feedback mode:

/set feedback

Setting the feedback mode:

/set feedback verbose
/set feedback normal
/set feedback concise
/set feedback silent

Loading Scripts

A JShell script is a sequence of snippets and JShell commands in a file, one snippet or command per line.

Starting JShell with executing a script:

$ jshell <file>

Reading and executing a script in JShell:

/open <file>

Important Notes

Code in the default package can't be accessed from JShell.

Tool Support

References

Java Shell User's Guide, Release 21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment