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

JShell Step by Step

October 24, 2017
Amit Nijhawan
Related topics:
Java

    Java 9 has added the following new features:

    1. Java 9 REPL (JShell)
    2. Factory methods for immutable List, Set, Map, and Map.Entry
    3. Private methods in Interfaces
    4. Java 9 module system
    5. Process API improvements
    6. Try with Resources improvement
    7. CompletableFuture API improvements
    8. Reactive Streams

    I am going to explore JShell in this blog.

    Steps to follow:

    1. Download Java 9 from scratch and install.
    2. Run the shell.
    3. Get help from Java 9's REPL.
    4. Run a few calculations in JShell.
    5. Define a function and use it.

    Step 1: Download Java 9

    • Go to: http://jdk.java.net/9/ and download on the jdk 9.
    • Verify you have it installed or not.
    [anijhawa@anijhawa ~]$ java -version
    java version "9-ea"
    Java(TM) SE Runtime Environment (build 9-ea+165)
    Java HotSpot(TM) 64-Bit Server VM (build 9-ea+165, mixed mode)
    

    Step 2: Run JShell

    To connect jshell, type jshell in the terminal.

    [anijhawa@anijhawa ~]$ jshell
    | Welcome to JShell -- Version 9-ea
    | For an introduction type: /help intro
    jshell>
    

    Step 3: Get Help

    • Type /help on the terminal and get a list of things can do with JShell:
    jshell> /help
    | Type a Java language expression, statement, or declaration.
    | Or type one of the following commands:
    | /list [<name or id>]
    | list the source you have typed
    | /edit <name or id>
    | edit a source entry referenced by name or id
    | /drop <name or id>
    | delete a source entry referenced by name or id
    | /save [-all] <file>
    | Save snippet source to a file.
    | /open <file>
    | open a file as source input
    | /vars [<name or id>]
    | list the declared variables and their values
    | /methods [<name or id>]
    | list the declared methods and their signatures
    | /types [<name or id>]
    | list the declared types
    | /imports
    | list the imported items
    | /exit
    | exit jshell
    | /env [-class-path <path>] [-module-path <path>] [-add-modules <modules>] ...
    | view or change the evaluation context
    | /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]...
    | reset jshell
    | /reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]...
    | reset and replay relevant history -- current or previous (-restore)
    | /history
    | history of what you have typed
    | /help [<command>]
    | get information about jshell
    | /set editor|start|feedback|mode|prompt|truncation|format ...
    | set jshell configuration information
    | /? [<command>]
    | get information about jshell
    | /!
    | re-run last snippet
    | /<id>
    | re-run snippet by id
    | /-<n>
    | re-run n-th previous snippet
    |
    | For more information type '/help' followed by the name of a
    | command or a subject.
    | For example '/help /list' or '/help intro'.
    |
    | Subjects:
    |
    | intro
    | an introduction to the jshell tool
    | shortcuts
    | a description of keystrokes for snippet and command completion,
    | information access, and automatic code generation
    | context
    | the evaluation context options for /env /reload and /reset
    
    • From /help command one of interesting command/imports, please try this and see the by default imports below:
    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.*
    

    Step 4: Do some basic calculations.

    jshell> 2+2
    $1 ==> 4
    
    jshell> 4%7
    $2 ==> 4
    
    jshell> 14&3
    $3 ==> 2
    
    jshell> $2// we got variable number two!
    $2 ==> 4
    

    Step 5: Multiply Function Code

    Let's define a function that takes an int and square it, then let's call it.

    jshell> int squareThis(int i){return i*i;}
    | created method squareThis(int)
    
    jshell> squareThis(5)
    $6 ==> 25

    To build your Java EE Microservice visit WildFly Swarm and download the cheat sheet.

    Last updated: November 15, 2018

    Recent Posts

    • 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

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    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.