wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How can I clear the screen? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I clear the screen?" Watch "How can I clear the screen?" New topic
Author

How can I clear the screen?

Ilya PA
Greenhorn

Joined: Sep 05, 2001
Posts: 15
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

Joined: Jan 16, 2001
Posts: 349
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

Joined: Sep 05, 2001
Posts: 15
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

Joined: Aug 17, 2001
Posts: 109
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


Sun Certified Programmer on the Java 2 Platform
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Sep 07, 2001
Posts: 53
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

Joined: Jun 26, 2001
Posts: 149
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

Joined: Jan 27, 2001
Posts: 5089
It prints 50.
see for youself :

Tom Ben
Ranch Hand

Joined: Aug 17, 2001
Posts: 109
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

Joined: Sep 07, 2001
Posts: 53

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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
James Gray
Ranch Hand

Joined: Sep 10, 2001
Posts: 30
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

Joined: Sep 25, 2001
Posts: 2
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

Joined: Feb 06, 2001
Posts: 358
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" ) ;


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
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

Joined: Jun 07, 2000
Posts: 87
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

Joined: Aug 03, 2001
Posts: 64
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


Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I clear the screen?