Adblocker detected! Please consider whitelist or disable this site.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

To keep the site operating, we need funding, and practically all of it comes from internet advertising.

If you still want to continue, Please add techgeeknext.com to your ad blocking whitelist or disable your adblocking software.

×
Java Shell Tool (2024) | TechGeekNxt >>


Java Shell Tool (2024)

What is Jshell?

It's an interactive Java Shell tool that enables us to execute Java code from the shell and instantly displays output. JShell is the REPL(Read Evaluate Print Loop) tool, run from command line.
In most programming languages, shell or REPL are a well recognized tool. Generally, these are popular for scripting languages like Python or Node, but more recently JVM languages such as Clojure and Group have also been adopted. Finally, the launch of Java 9 introduced this Java-language shell feature in the form of JShell.

Getting Started with JShell

Starting JShell - jshell -v Open the jshell from the command prompt in verbose mode.
D:\>jshell -v
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro
 
Stopping JShell - jshell -v
jshell> /exit
|  Goodbye
 

Note:

Any information comes after '|'(pipe) symbol, it gives information to user from jshell.

Simple Examples

jshell> 20+30 $1 ==> 50
|  created scratch variable $1 : int

jshell> 50-30*7 $2 ==> 140
|  created scratch variable $2 : int 

Default Imports

The following packages are available to the Jshell by default and we don't have to import them. We can search with the /imports command.
jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

Arraylist Example:


 jshell> Arraylist<String> li=new Arraylist<String>();
 li ==> []

jshell> li.add("John");li.add("Berry");li.add("Teena");
$2 ==> true
$3 ==> true
$4 ==> true

jshell> li
li ==> [John, Berry, Teena]

jshell> li.isEmpty()
$6 ==> falise

jshell> li.get(2)
$7 ==> "Teena"

jshell> li.get(10)
|  java.lang.IndexOutOfBoundsException thrown: Index 10 out-of-bounds for length 3

jshell> li.size()
$9 ==> 3

jshell> if(li.isEmpty()) System.out.printlin("Empty");else System.out.println("Not Empty");
Not Empty

jshell> for(int i =0;i<10;i=i+2)System.out.println(i)
0
2
4
6
8

Exception Example

In our program we have to handle either try-catch or by throwing keyword if there is a chance of checked exceptions. If not, we will get an error in compile time.

Java Program
import java.io.*;
 class HelloWorld
 {
     public static void main(String[] args)
     {
      PrintWriter printWriter=new PrintWriter("notes.txt");
      printWriter.println("Hello World");
     }
  }   
Now compile the program using javac HelloWorld.java, it will throw FileNotFound Exception as given below.
HelloWorld.java:6: error: unreported exception FileNotFoundException
Java Program in Jshell: Here, in Jshell we don't have to write public class name and main, just provide the main business logic. In Jshell, we dont't have write try-catch or throw explicitly, Jshell do it by itself.
Internally jshell use java compiler to check syntax.
jshell> PrintWriter printWriter=new PrintWriter("notes.txt");
printWriter.println("Hello World");
printWriter.flush();
printWriter ==> java.io.PrintWriter@e25b2fe
 
ADVERTISEMENT