How to save, edit, and drop a snippet in JShell in Java 9?

JavaObject Oriented ProgrammingProgramming

Java Shell or JShell is an official REPL(Read-Evaluate-Print-Loop) introduced with Java 9. It provides an interactive shell for quickly prototyping, debugging and without the need for the main() method or without the need to compile the code before executing it. JShell is easily started by typing "jshell" in the command prompt.

 Save a snippet

We can save a snippet source to a file by using "/save [-all|-history|-start] " command.

C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> /save C:\Users\User\jshell.txt

The above code creates a new "jshell.txt under the specified path.

Edit a snippet

We can also edit the code in an external editor by using the "/edit " command.

C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> /edit
| created method empName(String,String)

This launches the JShell edit pad where we can edit the code snippet and save it.

Jshell Edit Pad

Drop a snippet

Similarly, we can also drop a particular snippet by using the command "/drop <id>"

Output

jshell> /drop 1
| dropped method empName(String,String)

jshell>

The above command drops a snippet.

raja
Published on 21-Feb-2020 06:57:11
Advertisements