Java JShell or Java Shell tool introduction :
Java JShell was introduced in Java 9. It is an interactive tool for learning Java. It is a Read-Evaluate-Print Loop (REPL) that can be used to evaluate different Java expressions on the terminal.
JShell was introduced mainly to improve the Java learning experience for beginners. Previously, if you want to run one simple Java program, you need to write, compile and run the program even if it is only a one liner. After the JShell was included with Java 9, you can run any statement, declaration or expressions directly on the terminal and check the result without the need to write the full code.
If you are familiar with any other programming language like Python or Scala, Java shell is similar to the interactive shells used in these programming languages.
Benefits of using Java shell :
The following are the benefits of JShell :
-
It makes the Java learning more easy and interactive. We can easily run and check the output of Java statements directly on the terminal.
-
Using JShell, we can test part of our Java program. We can copy-paste specific part of a large project and check the result on the terminal.
-
The above point (2) allows us to debug and fix errors in a Java program easily. Instead of compiling and running a full program each time to debug errors, we can take help of the JShell.
-
JShell is best for trying different unfamiliar APIs before using it on a project.
Remember that JShell is not for replacing an IDE in Java development. We cannot create a full project in JShell. But it is good for beginners and to test few Java snippets quickly.
How to start JShell in terminal :
Starting JShell in terminal is easy. Only thing is that your system Java JDK version should be 9 or above. If you have older JDK version installed, you can upgrade it from here.
Now, to start the JShell, open your terminal or command prompt, type jshell and hit enter. If it is showing as “command not found”, you need to set the path on your system.
If everything goes fine, you will see the result something like below :
The above image is for Mac but the result will be similar on both Windows and linux.
You can also use jshell in verbose mode like below :
And finally, for quitting the jshell, type /exit and hit enter :
That’s all for JShell. Keep visiting our blog, we will publish more articles on JShell soon.
As always, if you have any queries or if you want to say something that will bring a smile on my face , drop one comment below :)