How can we customize the start of JShell in Java 9?


JShell is an interactive REPL tool to execute and evaluate simple Java programs like variable declarations, statements, expressions, and etc.

When the JShell tool launched, the code has pre-loaded by default. To display this code, we just launch the command "/list -start". It is possible to ask JShell to load them automatically when it starts by using the command: "/set start [-retain] [Type]". The first option "-retain" tells JShell to record the desired [Type] startup for the next JShell sessions. If we don't specify it, the default startup can be launched when opening a new session

  /set start [-retain] -File
  /set start [-retain] -Default
  /set start [-retain] -none
  • /set start [-retain] File: This command tells JShell that when it starts, it must load the content of the file (this file contains code for creating variables, methods, classes, etc.).
  • /set start [-retain] -none: This command tells JShell not to load any code/command when it starts.
  • /set start [-retain] -Default: This command resets the start of JShell to its default value.


In the below code snippet, we can use "/set start [-retain] -File" command,

jshell> /list -start

  s1 : import java.io.*;
  s2 : import java.math.*;
  s3 : import java.net.*;
  s4 : import java.nio.file.*;
  s5 : import java.util.*;
  s6 : import java.util.concurrent.*;
  s7 : import java.util.function.*;
  s8 : import java.util.prefs.*;
  s9 : import java.util.regex.*;
  s10 : import java.util.stream.*;

jshell> /set start -retain save.jsh

jshell> /exit
|   Goodbye

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

jshell> /list -start

  s1 : int x = 20;
  s2 : double y = 30;
  s3 : public int sum(int a, int b) {
          return a + b;
       }
  s4 : String str = "TutorialsPoint";


In the below code snippet, we can use "/set start [-retain] -none" command.

jshell> /set start -retain -none

jshell> /exit
|   Goodbye

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

jshell> /list -start


In the below code snippet, we can use " /set start [-retain] -Default" command.

jshell> /set start -retain -default

jshell> /exit
|   Goodbye

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

jshell> /list -start

  s1 : import java.io.*;
  s2 : import java.math.*;
  s3 : import java.net.*;
  s4 : import java.nio.file.*;
  s5 : import java.util.*;
  s6 : import java.util.concurrent.*;
  s7 : import java.util.function.*;
  s8 : import java.util.prefs.*;
  s9 : import java.util.regex.*;
  s10 : import java.util.stream.*;

Updated on: 13-Apr-2020

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements