# How to clear screen in Java?



## redhat (Jun 23, 2007)

Hi,
I am creating a project in Java for my std. 10th Boards...

I need to know how to clear(flush) the command prompt screen from a code.
Like its "clrscr()" in C.

Actually its a menu driven program and I need to clear the screen after an option is selected, to make clean space for the next menu...

waiting for a reply...........


----------



## QwertyManiac (Jun 23, 2007)

By default there is no function in Java that does this, cause its platform independent.

The only way to do so is to call up the CMD's '_cls_' function and this thing makes the code messy and sometimes doesn't work out on the same window as your program. But anyway, the code for that'd be:


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

Or maybe you can print a few blank lines via a loop to fake it. But trust me, thats way better than calling up the command's '_cls_' and making your code messy all over.

Also, it isn't really that important to refresh the screen and start all over, just to make it look better. Instead, concentrate on your functionalities of the program.


----------



## ~Phenom~ (Jun 23, 2007)

^^yeah . I was also  about to say:"If U cant make it , fake it."  A year back , I also wanted to do the same as redhat and used blank lines in loops . I was not aware of the method qwerty said, donno if it really works.


----------



## bazigaar_no_1 (Jun 24, 2007)

exactly... that's whay i did in my 10th standard project... create a new class (with a constructor) just having a loop that prints 30 new blank lines... so everytime you wanted to clear the screen just make an object of that class ..


----------

