How can we load a source code into JShell in Java 9?

JavaObject Oriented ProgrammingProgramming

JShell is an interactive tool for learning Java, and it is a REPL(Read-Evaluate-Print-Loop) that evaluates declarations, statements, and expressions.

While leaving a JShell session, we want to reuse the code previously entered into a new session. This can be done by using the command: /open [File_Path]. This command will load all code and internal commands found in file[File_Path] supplied as an option.

In the below code snippet, we can use the "/open [File_Path]" command to load source code from the directory with the ".jsh" extension.

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

jshell> x
|  Error:
|  cannot find symbol
|   symbol: variable x
|  x
|  ^

jshell> y
|  Error:
|  cannot find symbol
|   symbol: variable y
|  y
|  ^

jshell> /list

jshell> /open C:\Users\User\Desktop\save.jsh

jshell> /list

  1 : int x = 15;
  2 : double y = 25.0;
  3 : public int sum(int a, int b) {
         return a + b;
      }

jshell> x
x ==> 15

jshell> y
y ==> 25.0

jshell> int result = sum(5, 7)
result ==> 12
raja
Published on 07-Apr-2020 14:33:07
Advertisements