• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to clear console screen?  RSS feed

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to clear console screen ? I am using Linux machine.

I googled and found 2 ways but nothing is workable. Please suggest anything better.

1) Runtime.getRuntime().exec("clear"); // this is not working. Not clearing the screen

2) for ( int i = 0; i < SOME_NUMBER; i++)
System.out.println();

2nd way is working but i think it is not a good option as i need to move scroll bar again and again.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a FAQ on this exact topic, due to the large number of times it has been asked (and answered).
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with our FAQ and see whether that helps.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks guys i checked the shared link. Its giving me same advice as i googled. In addition to it it also suggesting to install some packages but i do not want to install any other package. So, i guess, my 2nd option only left..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does "clear" work when you just run it from the command line?

If so, you should figure out why your Runtime...etc doens't work.

If not, you need to figure out what does, and use that.

I also don't understand what is wrong with option 2. Why do you need to move the scroll bar? It generally helps us help you if you TellTheDetails...
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does "clear" work when you just run it from the command line?

If so, you should figure out why your Runtime...etc doens't work.


clear is working on command line but it is not working through Runtime. Let me check this.


I also don't understand what is wrong with option 2. Why do you need to move the scroll bar? It generally helps us help you if you TellTheDetails...


There is no as such requirement but i thought to use something whose output is like "clear"
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I found another solution. We need to use ASCII escape sequence.

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

'H' means move to top of the screen
'2J' means "clear entire screen"

It works exactly clear works. Meantime i am also trying to figure out why clear is not working.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what does \033 mean? I can't be bothered to convert that to proper numbers (hex) to look it up on Unicode.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And what does \033 mean? I can't be bothered to convert that to proper numbers (hex) to look it up on Unicode.

\033 in ASCII is ANSI escape code (ESC): wiki.

Hope I didn't confused ANSI and ASCII ;). I never could remember them correctly ;)
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Paul Clapham
Sheriff
Posts: 22843
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:
Does "clear" work when you just run it from the command line?

If so, you should figure out why your Runtime...etc doens't work.


clear is working on command line but it is not working through Runtime. Let me check this.


Runtime.exec() creates another window (assuming you configured it correctly) and then "clear" clears that window (assuming you did it correctly). In any case it doesn't clear the window of the code it was called from. Does the FAQ entry not say that?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime.exec() creates another window (assuming you configured it correctly) and then "clear" clears that window (assuming you did it correctly). In any case it doesn't clear the window of the code it was called from. Does the FAQ entry not say that?


FAQ does not mention that. Thanks Paul for clarifying..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!