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 "Test.java" 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 "Test.java" file into JShell by using the below command
C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> /open c:\\temp\\Test.java 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 "Test.java" 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.