20

I didn't find any command to clear Java-9 JShell console. I also tried to clear the JShell Console through this program, but it doesn't work either.

import java.io.IOException;

class CLS {
    public static void main(String... arg) throws IOException, InterruptedException {
        new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
    }
}

I think we don't have that functionality available yet in the early access. Anyone got an idea?

Maroun
  • 94,125
  • 30
  • 188
  • 241
B. S. Rawat
  • 1,874
  • 3
  • 22
  • 34
  • There is open bug to address this issue: https://bugs.openjdk.java.net/browse/JDK-8153762?jql=project%20%3D%20JDK%20AND%20component%20%3D%20tools%20AND%20Subcomponent%20%3D%20jshell%20ORDER%20BY%20status%20DESC – B. S. Rawat Apr 29 '16 at 17:58
  • 1
    It's not a *bug*, it's a feature request. My answer gives a workaround. – Maroun May 01 '16 at 12:32
  • You are right @MarounMaroun, Sorry for wrong wording. it is definitely a future request. – B. S. Rawat May 02 '16 at 18:52

7 Answers7

13

Use the Control Key+L or l (lower case L) to clear the JShell console.

7

You can use the following command (reference):

System.out.print("\033[H\033[2J");

Example:

-> System.out.println("Hello, JShell!")
Hello, JShell!

-> System.out.println("How are you?")
How are you?

-> System.out.print("\033[H\033[2J");

Now you'll have a clear console.

Maroun
  • 94,125
  • 30
  • 188
  • 241
3

In macOS terminal, you could use the CMD+K shortcut to clear the JShell console.

Hexfire
  • 5,945
  • 8
  • 32
  • 42
Shant Dashjian
  • 888
  • 11
  • 20
2

There is an issue about this Bug jshell tool: Clear screen command

for now, first exit jshell by

 /exit 

then

 cls

if you want to reset use

 /reset
Prags
  • 2,457
  • 2
  • 21
  • 38
i.karayel
  • 4,377
  • 2
  • 23
  • 27
0
  • I have added clear console command in JShell. You can download it from Try artifact

-> /cls

  • It also supports maven in JShell.
B. S. Rawat
  • 1,874
  • 3
  • 22
  • 34
0

OPTION A:

Create a method: void cls() { for( int i=0; i < 50; i++ ) { System.out.println(); } } and call it.

OPTION B:

To retain it for future sessions also, append the method to a startup script as follows:

  1. Create and save a file with following(retain any of yours):

/open DEFAULT void cls() { for( int i=0; i < 50; i++ ) { System.out.println(); } }

  1. Run the script: /set start -retain <PATH+FILE_NAME[+EXT]>
  2. Run command(save your work before using this command): /reset
  3. Call the method.

NOTE: Call cls() , not just cls.

EDIT: Edit the number 50 as per your screen.

lupchiazoem
  • 8,026
  • 6
  • 36
  • 42
0

Use CTRL+L to clear Jshell screen in Windows 11 OS.