← Back to list

JShell Command Line Tool — Powerful Game Changer for Java

Hello Java developers, today I am going to discuss about JShell command line tool. Let’s begin without losing time.

Mammad Yahyayev in Javarevisited · 2023-05-30 07:02 · 0 claps · 6.2 min read paywalled
#java #jshell #technology #programming #command-line
Open on Medium ↗
Wiki topics: 💻 · Programming 🥊 · Combat Sports

What is JShell Command Line Tool?

Hello Java developers, today I am going to discuss about JShell command line tool. Let’s begin without losing time.

Introduction

JShell is introduced in Java 9, but still a lot of developers don’t know what it is and how they can use it very effectively. I believe at the end of the article you will not lose the time to try it on your own.

What is JShell?

JShell stands for Java Shell Tool. It is an interactive tool for practicing Java codes from command line. When you write a code in JShell, it immediately shows the result on console.

However, don’t confuse the JShell with IDE or any text editor. It is a console tool and can’t replace them. JShell is actually very useful for following scenarios:

  • Practice Java language features
  • Teach code to your classmates or students.
  • Creating one class programs fastly

and so on. Now let’s move on to see JShell in action.

How to Start / Exit from JShell Command Line Tool?

CAUTION: Before we begin, you need to have JDK 9 or above installed on your machine.

Staring JShell is so simple, open the terminal and type jshell.

In addition, if you want to start it on verbose mode type following command:

jshell -v

In order to stop JShell, simply type /exit. We are good to move to try some commands inside of JShell.

JShell Basic Operations

Let’s do basic math calculations such as addition, subtraction, multiplication, and division.

If you look closely, you are going to discern, it assigns the result of operations to variable, so you can access the result of it later on JShell. Let’s see how.

In our examples, $1 variable refers to 4. 4 100 equals to 400*. It is quite easy and simple.

I think you already see the power of JShell. If you want to do these calculations in any IDE, you need to do following steps:

  1. Create a project.
  2. Create a class.
  3. Add main method.
  4. Write code.
  5. Execute code.

JShell makes our life much easier.

How to Write Multiline Code in JShell?

Next, we are going to talk how to you can write codes that contains multiple lines in JShell. Let’s assume you want to write a conditional statement in JShell. Of course you can write it one line, but it will be more readable if we split it into multiline.

If you press enter on terminal, JShell will take that as an operation, therefore if the syntax is incorrect, JShell will print an error message. In order to write a multiline code, press CTRL + Enter.

When you write multiline code, you need to put semicolon (;) at the end of each line like in Java. As you can see on the image above, I put semicolon at the end of print statement. And it automatically executes and print the result (true) on the console.

Writing multiline code can be a little bit challenging at first, but if you get it right, you will do that very quickly. Now let’s see how we can write if, else if, and else at the same time.

In this case, I defined age variable and assign the value, then wrote the conditions. It is no need to explain the code itself, and you can see the result on bottom of the image. Writing multiline code takes a little bit more time.

How to Create a Method in JShell?

If we can write multiline code in JShell, then can we create a method in JShell?. Of course, you can. Let’s see how.

You don’t need to write public or static, just write return type, method name, and parameters if there are. Now, let’s look at how we can call this method.

Very simple. Let’s write a method that has return value.

Now, let’s see how it is easy to print the result.

As you can see, no need to assign the return value to any variable, just call the method.

How to Create a Class in JShell?

Now we already know, we can create a method with multiline code. It is possible to create a class in JShell. Here’s a simple Student class.

After creating a class, we can create an instance of Student class.

As you can see on the image, we assign values to name and surname fields, and call fullname() function.

Another great thing, you don’t need to type all of them, you can press TAB for auto completion.

Furthermore, you can call some of the built-in Java classes. For instance, let’s generate a random number by using Math class.

How to create switch statement in JShell?

We’ve already seen how to create method, class, conditional statements in JShell. Now let’s look at how we can create switch statement.

As you can see on the image there is no difference. Firstly, we create age variable and assign 20. Secondly, we create our switch statement and its cases.

How to create traditional for loop in JShell?

It is very simple as the others. And it will print all the values.

How to create for each loop in JShell?

We’ve already seen how to create traditional for loop, for each is pretty similar.

First, we create a names array with 3 values. Then we create our for each loop, and it printed all the names on console.

JShell Commands

We tried different things on JShell. Now it is time to see JShell built in commands. There are several commands, you needn’t to learn all of them, but a few can be very useful.

  • vars
  • methods
  • list
  • imports
  • save
  • reload

vars

vars command shows you the variables within current session.

methods

methods command gives you the list of methods within a session

list

list command shows all the operations that you did in current session.

imports

imports command shows all the imports that you have in current session.

As you can see on the image above, those imports statements are added automatically in each JShell session. Therefore, we could easily call functions of Math class.

save

save command will save all the codes that are written in this session. In order to save the code into any file, you have to give the name of the file, and it will automatically create the file in the current working directory.

/save code

It will only add the codes that you wrote. Import statements were not added.

reset

As the name suggest, reset command clears the state for current session.

As you can see on the image, after reset command, I executed list and vars commands, but get nothing as result.

Conclusion

That is for JShell command line tool, I hope you enjoyed by reading article. If you find it useful, check out my other articles.

Take care of yourself, see you in upcoming articles.


메타데이터
post_id
7f3593d62fc0
slug
jshell-command-line-tool-powerful-game-changer-for-java-7f3593d62fc0
url
https://medium.com/javarevisited/jshell-command-line-tool-powerful-game-changer-for-java-7f3593d62fc0
canonical_url
https://medium.com/javarevisited/jshell-command-line-tool-powerful-game-changer-for-java-7f3593d62fc0
author_url
https://medium.com/@mammadyahya
status
ok
fetched_at
2026-07-25 17:27:11