Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I clear the screen?

 
Ilya PA
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi... I was wrighting a program, and it has several ouputs. I was using
System.out.println("Bla bla bla");
But when I run it in the second time the ouput from the first run stays on the screen, how can I clear it before running? Is there any method like "clrscr;" in Pascal?
 
Rashid Ali
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ilya
I came to know the way how to clear the screen on the DOS window through this forum. There is not a pre-defined method as you mentioned but this may be called from DOS thru Runtime class.
One of the way mentioned in that email is as follows. You can search that message about which I mentioned earlier for another way.

Good luck
Rashid Ali
 
Ilya PA
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, for the advice, but it just doesn't seem to work out that way... dunno why. But thanx anyway!
 
Tom Ben
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same problem with the call the command line argument "cls" thru java this is what I did instead.
private static void clear() {
for (int y = 0; y < 25; y++)
System.out.println("\n");
}
This method will place 25 newlines to the screen. Since the screens are '24'x'80' This seems to work ok. Now if someone can tell me why I can call a batch file in dos thru java and it works great but can not call a script thru HP-UX 11.
------------------
Sun Certified Programmer on the Java 2 Platform
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am also a student of java.
i agree with tom.
but i think that's not the way.
use flush();
i will send you code of that.
within a day.
 
parul patidar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will following code place 25 newline characters or 50 new line characters??
Originally posted by Tom Ben:
I had the same problem with the call the command line argument "cls" thru java this is what I did instead.
private static void clear() {
for (int y = 0; y < 25; y++)
System.out.println("\n");
}
This method will place 25 newlines to the screen. Since the screens are '24'x'80' This seems to work ok. Now if someone can tell me why I can call a batch file in dos thru java and it works great but can not call a script thru HP-UX 11.

 
timothy zimmerman
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that 50 new lines would be written. One for each println() and one for each new line ( "\n" ).
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints 50.
see for youself :

 
Tom Ben
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it prints 50 sorry I was in a hurry and typed println not print. Thanks for clearing that up. Did that flush() option work?
------------------
Sun Certified Programmer on the Java 2 Platform
 
parul patidar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

waiting for code

Originally posted by dwivedi_sachin5:
i am also a student of java.
i agree with tom.
but i think that's not the way.
use flush();
i will send you code of that.
within a day.

 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
flush() is used in IO to clear the streams. It will not clear a console - only a "pipe".
It depends on your OS. You need to use whatever command works from the propmt on that OS.
This works for NT/Win2000 and I think 98
Runtime.getRuntime().exec( "cmd /c cls" ) ;
Before that (win95) you had to use
Runtime.getRuntime().exec( "command /c cls" ) ;
I am not sure what works for Unix.
This obviously will make the app OS specific, therefore a better bet is just to print out a bunch (maybe 30 or so) of newline characters to the console, which forces the stuff on the console to scroll up.
 
James Gray
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy hints at it above, but just to be clear Java's console is not yet robust enough to provide screen clearing functionality.
 
jungil
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rashid Ali:
[B]Dear Ilya
I came to know the way how to clear the screen on the DOS window through this forum. There is not a pre-defined method as you mentioned but this may be called from DOS thru Runtime class.
One of the way mentioned in that email is as follows. You can search that message about which I mentioned earlier for another way.

Good luck
Rashid Ali[/B]

When I run your program, there are some errors.why?
Exception in thread "main" java.io.IOException: CreateProcess: command /c cls er
ror=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Unknown Source)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Test.main(Test.java:3)
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy pointed out that the DOS command are specific to the Operating system. Try using cmd instead of command.

This works for NT/Win2000 and I think 98
Runtime.getRuntime().exec( "cmd /c cls" ) ;
Before that (win95) you had to use
Runtime.getRuntime().exec( "command /c cls" ) ;
 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am using Windows 98 and using either of the commands,
Runtime.getRuntime().exec( "command /c cls" ) ;
or
Runtime.getRuntime().exec( "cmd /c cls" ) ;
is throwing an IOException.
Kezia.
 
shilpa kulkarni
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cindy Glass:
This works for NT/Win2000 and I think 98
Runtime.getRuntime().exec( "cmd /c cls" ) ;

I tried this - the program does not throw any exception, but does not clear the screen either!

 
Ian Darwin
author
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There have been quite a number of responses quoting a way to "clear the screen", but everything about the screen is operating-system specific (and even version-specific as we've seen in the discussions of MS-Windows-95 vs MS-Windows-NT).

A more portable way is to create a JFrame with a JTextArea or JTextPane in it. You then append to it instead of printing.

You can call its setText method with a null String, i.e.,
<pre>myTextArea.setText("")</pre> to clear then screen.

------------------
Ian Darwin,
Author of Java Cookbook: Solutions and Examples for Java Developers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic