top of page

How to JShell

JShell is an incredibly useful command line tool which I use every single day to execute short Java code snippets, to compute result arithmetic operations and to verify all the single line code examples on this website.

Getting started with JShell

Open your console, terminal or command prompt depending on what your operating system is. A window should open and you may notice that when you start typing whatever you type shows up on said window. Type in the single word "jshell" and press enter. If this produces an error then you have not yet installed openjdk. For MacOS, Linux and for Windows.

After following instructions that may or may not have worked because of version incompatibility you should now have a working JShell. 

Exiting JUnit


You might now ask yourself how to close the JShell again, as almost anything you type will be interpreted as Java code and either produce an error if it is not a valid Java statement or execute the given Java statement. To escape this you can use /exit which tells JShell that you are interested in leaving the JShell environment.

In general you can type /help to see a list of all commands you can use in JShell.

JShell /list

One might want to list all statements which have been executed so far after having worked with JShell for some time without closing it. Typing "/list" into the console and pressing enter will allow you to that.

This will produce a numbered list of all Java statements executed until now in the current JShell environment. Once you close the JShell though this information will be lost.

JShell /edit

One can go even further using "/edit" which allow you to view an editable list of all statements executed so far in a pop-up window. You can edit the statements as long as they are change to valid Java statements. 

The pop-up window will be called "JShell Edit Pad" or similarly. While this window is open you cannot type anything into the console anymore until you close the edit pad. For the changes to have an effect you need to click accept which will keep the edit pad opened or exit which will save the changes and the close the edit pad.

JShell /save and /open

You might have created methods inside JShell and want to save these for future use. This will save the JShell environment in the format of the edit pad file.

You need to specify a name after typing "/save", e.g. "/save myFancyFile". You can then open said file using "/open" at a later point in time from within the JShell.

JShell multiline statement


You can do almost anything you can do in Java also in JShell. You can create entire classes in JShell by simply typing it out line-by-line. Be careful that you type every line correctly however as JShell will let you know that there is a mistake only when you are finished typing the entire code and not during the writing process.

For any serious program you should however use a programming environment like the Eclipse IDE and not JShell.

bottom of page