Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

10 things developers need to know about JShell

April 5, 2019
Rahul Kumar
Related topics:
Java
Related products:
Red Hat Enterprise Linux

Share:

    JShell was introduced in JDK 9 as part of Java Enhancement Proposal (JEP) 222 under project Kulla. Many programming languages, such as JavaScript, Python, Ruby, etc., provide easy-to-use, command-line tools for their execution, but Java was still missing such a utility. So, JDK 9 introduced the Java shell (JShell) tool.

    I discussed the basics of JShell (which is a Read-Evaluate-Print-Loop; REPL) in a previous article. In this article, I'll cover advanced concepts in JShell that users should know for rapid development.

    1. Re-declaration of variables

    In Java, it is not possible to re-declare a variable. With the help of JShell, however, you can always re-declare the variable based on needs. Note that this is applicable for both primitive as well as for reference type variables. In fact users can re-declare any construct as many times as they want.

    Example:

    jshell> String str="Hello"
    str ==> "JShell"
    jshell> Integer str=10
    str ==> 10

    2. Scratch variables

    Any expression evaluation from JShell command line is assigned to some variables if not explicitly assigned by user. Such variables are called scratch variables. Example:

    jshell> "Hello"+"JShell"
    $1 ==> "HelloJShell"

    Note that in order to know the type of variable or more details about expression evaluation, we can set the feedback mode to verbose as below:

    /set feedback verbose
    jshell> 60+10
    $2 ==> 70
    |  created scratch variable $21 : int

    To come out of verbose mode, set the feedback mode to normal:

    /set feedback normal

    3. Forward referencing in JShell

    Forward referencing in JShell allows you to call constructs up front even though they do not exist. For example, suppose there is a method named greet() as below. Notice that greet() internally calls another method named greetHelloWorld(), which is not declared yet. Creation of greet() is successful, but it can't be invoked until greetHelloWorld() is declared. This is called forward referencing in JShell.

    Example:

    jshell> public void greet(){
    ...> greetHelloWorld();}
    |  created method greet(), however, it cannot be invoked until method greetHelloWorld() is declared               jshell> greet()
    |  attempted to call method greet() which cannot be invoked until method greetHelloWorld() is declared
    jshell> public void greetHelloWorld(){
    ...> System.out.println("Hello World");}
    |  created method greetHelloWorld()
    jshell> greet()
    Hello World

    4. Exception Handling in JShell

    Example:

    jshell> int divide(int a,int b) throws IOException{
    ...> if(b==0){
    ...> throw new IOException();
    ...> }
    ...> return a/b;
    ...> }
    |  created method divide(int,int)
    jshell> divide(1,0)
    |  java.io.IOException thrown:
    |        at divide (#2:3)
    |        at (#3:1)

    Notice that we are not catching any exceptions thrown by divide method; JShell takes care of that. Also notice that we are not importing IOException class, but the code is compiled and executed just fine. The reason for this is that, for any JShell session, some packages are imported by default. To check all the packages that are by default imported in any JShell Session:

    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.*
    |    import java.io.IOException

    5. Persistency behavior of instructions in JShell session

    All instructions in JShell Session are not persistent by default. They are transient in nature and are lost when a user exits from a JShell session.

    JShell, however, provides a way for users to save all the information in a particular JShell session and access that information in a different JShell session. This is very handy if users want to save the useful snippets from a JShell session and access them in a different JShell session.

    Example:

    jshell> String s="Hello"
    s ==> "Hello"
    
    jshell> int i=100;
    i ==> 100
    jshell> /save C:\data\mySession.jsh
    jshell> /exit
    |  Goodbye
    
    λ jshell
    |  Welcome to JShell -- Version 9.0.4
    |  For an introduction type: /help intro
    jshell> /vars
    jshell> /open C:\Data\mySession.jsh
    jshell> /vars
    |    String s = "Hello"
    |    int i = 100

    6. Using external libraries

    There are many useful third-party open source libraries. Typically, developers keep those libraries in class-path of project and use it. But, in the case of JShell, using third-party libraries is quite easy.

    For example, suppose we want to use a String Utility class from a third-party library called Apache Commons Lang. Here is the syntax for keeping libraries in class-path:

    shell> /env --class-path <Relative Path of lib from where JShell is run>
    jshell> /env --class-path ../lib/commons-lang3-3.8.1.jar
    |  Setting new options and restoring state.
     import org.apache.commons.lang3.StringUtils;
    jshell> System.out.println(StringUtils.isEmpty(""))
    true
    jshell> System.out.println(StringUtils.isEmpty("hello"))
    false

    7. Using JShell specific commands and tools for quick development

    JShell comes with its own specific handy commands, which can be used for faster testing on the JShell Console. Below are some useful commands:

     /history - Prints all commands executed on JShell (Java Commands+ JShell specific commands)

    Example:

    jshell> String s ="Hello"
    s ==> "Hello"
    
    jshell> class Employee{
    ...> }
    |  created class Employee
    
    jshell> /vars
    |    String s = "Hello"
    
    jshell> /history
    String s ="Hello"
    class Employee{
    }
    /vars
    /history

     

     /list    - Prints all JAVA related commands executed in JShell. Notice that this list the command in Numerical              order of each command identifier. This identifier can be used to execute certain construct again.

    Example:

    jshell> /list
    1 : String s ="Hello";
    2 : class Employee{
    }
    jshell> /1
    String s ="Hello";
    s ==> "Hello"
    /reset   - Resets the state of current JShell session.
    CTRL+R   - For searching a particular command
    CTRL+S   - Performing Forward Search
    CTRL+C   - To exit from JShell session
    /exit    - To exit from JShell session
    /vars     - To list all variables inside current JShell session
    /imports  - To list all imports inside current JShell session
    /help    - To know more about JShell specific commands

    8. Tab completion in JShell

    JShell allows developers to complete their code constructs automatically using the Tab key.

    Example:

    Along with this, user can also see documentation for relevant packages using JShell:

    jshell> java.io
    io
    Signatures:
    java.io
    <press tab again to see documentation>

    9. Editing a construct in JShell

    It's very common during development to want to edit a previously executed command in JShell session. JShell provides a very handy command and editor for same.

    Example:

    /edit        - Edit all constructs in current JShell session
    /edit 1      - Edit only 1st construct (see from /list) in current JShell session
    /edit Person - Edit only Person class in current JShell session

    10. Using JShell programmatically

    JDK offers an API for programmers to access JShell programmatically rather than by using REPL. Please refer to the corresponding Java docs for more details.

    I've shown just a few advanced concepts in JShell in this article, and this is not the end. I also suggest readers go through the JShell docs for more information.

    Last updated: June 8, 2021

    Recent Posts

    • Cloud bursting with confidential containers on OpenShift

    • Reach native speed with MacOS llama.cpp container inference

    • A deep dive into Apache Kafka's KRaft protocol

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue