Skip to content
Sign upLog in
← Back to Community

How do you clear terminal in Java?

Profile icon
SriramManikand1

I need help with clearing the terminal/ console in java. Any help will be appreciated!

Answered by Funkus [earned 5 cycles]
View Answer
Voters
Profile icon
SriramManikand1
Comments
hotnewtop
Profile icon
Funkus

You can use this code in a method System.out.print("\033[H\033[2J");
System.out.flush();

Profile icon
AntonBut

Sadly I didn't find any good method. The clearScreen() method clears only like 20-30 lines of code, if you run it few times, it become work really slow. I wish replit will insert command that is equal to pressing trash basket button...

Profile icon
rss81

only the first method works on replit java

Profile icon
Bookie0

hi!

check this out: https://www.quora.com/How-do-I-clear-console-screen-CMD-screen-in-Java-Is-there-any-function-in-Java-like-clrscr-and-system-cls-in-C

bascially you can use this:

public static void clearScreen() { System.out.print("\033[H\033[2J"); System.out.flush(); }

or:

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

or

System.out.flush();

or

Runtime.getRuntime().exec("cls");

but i think the 1st one is the best ;)

Happy coding and hope this helps! =)