How to load a file into the JShell session in Java 9?

JShell is a new command-line interactive REPL (Read-Evaluate-Print-Loop) tool introduced in Java 9 to evaluate declarations, statements, and expressions written in Java. This tool also allows us to execute Java code snippets and get immediate results.

Some times, we have the code already written in java file and able to execute it into JShell. To load a file into the JShell tool, we can use the "/open" command.

For instance, I have created the "" file in the "c://temp" folder. Below is the code:

String s1 = "TutorialsPoint";
String s2 = "Tutorix";
String s3 = s1 + s2;

int sum(int a, int b) {
   return a + b;

int divide(int a, int b) {
   return a / b;

Now, we can able to load "" file into JShell by using the below command

| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> /open c:\temp\

jshell> /vars
|    String s1 = "TutorialsPoint"
|    String s2 = "Tutorix"
|    String s3 = "TutorialsPointTutorix"

jshell> /methods
|    int sum(int,int)
|    int divide(int,int)

The "/open" command has loaded the "" file into a session. The "/vars" command can be used to load the variables into a session and "/methods" command can be used to load the methods into a session.

Updated on: 13-Mar-2020


Kickstart Your Career

Get certified by completing the course

Get Started