Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

10 things developers need to know about JShell

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

    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

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.