tags:

views:

420

answers:

8

Is there any option to clear the screen in java as clrscr() in C.

+5  A: 

If you're talking about the console, then no. Writing to the console is just a special case of an output stream. Output streams don't know anything about the screen, as they can be just as easily redirected to a file or another system device.

Tenner
+2  A: 

If you're talking about a console application, then there isn't a clear screen option AFAIK. A quite dirty option would be to invoke the clear screen command of the underlying OS.

Then it's something like

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

for Windows or

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

for a load of other OS. You can find out the OS with System.getProperty("os.name").

msparer
`dirty` very `dirty` :D
Rakesh Juyal
it is not working. i tried in windows xp.
I think `cls` is a built-in function, not an actual executable.
Michael Myers
A: 

I wonder whether you could calculate the screen size in characters and then feed a bunch of backspace characters to standard output?

Derek Bennett
A: 

For Windows, Java Console API project provides functionality to determine console size and set cursor position. Clearing the screen is trivial with that. It's a version 0.2 now so it's not exactly production ready, but it works.

Alternatively, you can simply print out some new lines via System.out.println(). 640 should be enough for everybody :-) It's not the same as clearing screen, but for user's intents and purposes it'd do.

ChssPly76
A: 

On linux, you can do something like:

System.out.println("\f");

You can also use Jcurses

LB
+3  A: 

As dirty hacks go, I like msparer's solution. An even dirtier method that I've seen used (I would never do this myself. I swear. Really.) is to write a bunch of newlines to the console. This doesn't clear the screen at all, but creates the illusion of a clear screen to the user.

char c = '\n';
int length = 25;
char[] chars = new char[length];
Arrays.fill(chars, c);
System.out.print(String.valueOf(chars));
Bill the Lizard
I didn't dare to write this answer down. Nice :-)
LB
@LB: I admit this is probably the *worst* way to do it. Sometimes you just have to share your WTFs. :)
Bill the Lizard
How is this different than`System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");`which is one line instead of five?
Tenner
@Tenner: My code could be written as a function with the char and length passed in. Other than that, no real difference.
Bill the Lizard
+1 for awesome but evil.
DivineWolfwood
A: 

For any console which supports ANSI escapes the following would work (would e.g. work in Win98 console).

private final String ANSI_CLS = "\u001b[2J";
....
System.out.print(ANSI_CLS);
System.out.flush();
...

Starting with Win NT this won't work anymore and you can either

Otherwise you are out of luck.

And btw. you must keep in mind that System.out and System.err don't have to be console they could be set to what ever (writing into a file e.g.) an usecase where clearing the screen wouldn't make any sense at all.

jitter
A: 

you should give a try with JNA and try mapping native libraries:

  • on linux you must map C functions from ncurses library
  • on windows you must map functions from both msvcrt and kernel32, as clearly stated here

PS

let me known if you need some sample code

dfa