Clearing in Java

Can you clear the window in Java?

Page 1 of 1

5 Replies - 5735 Views - Last Post: 10 May 2010 - 01:53 AM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Clearing in Java

Posted 09 May 2010 - 04:11 PM

Simple question, is there anyway to clear the window in Java. Example: clrscr() in C++.
Is This A Good Question/Topic? 0
  • +

Replies To: Clearing in Java

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3196
  • View blog
  • Posts: 19,226
  • Joined: 14-September 07

Re: Clearing in Java

Posted 09 May 2010 - 04:22 PM

Short answer no, unless you do 20 newlines.

This should answer your question more specifically
Was This Post Helpful? 1
  • +
  • -

#3 pmiller624  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 02-May 10

Re: Clearing in Java

Posted 09 May 2010 - 06:09 PM

Like KYA said there is no inherit way to clear the screen in Java but one thing you can do is create a method for it.

Like so
public void clear(){
   System.out.println("\n\n\n\n\n\n\n\n\n\n");
}


Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Clearing in Java

Posted 09 May 2010 - 06:36 PM

View Postpmiller624, on 09 May 2010 - 08:09 PM, said:

Like KYA said there is no inherit way to clear the screen in Java but one thing you can do is create a method for it.

Like so
public void clear(){
   System.out.println("\n\n\n\n\n\n\n\n\n\n");
}



Actually, there IS a better way as described by cfoley: http://www.dreaminco...post__p__678239
Was This Post Helpful? 2
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12148
  • View blog
  • Posts: 45,161
  • Joined: 27-December 08

Re: Clearing in Java

Posted 09 May 2010 - 06:38 PM

Thanks for pointing that out Dogstopper. That's a very useful snippet.

@cfoley: You should submit your solution to the Java snippets section.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Clearing in Java

Posted 10 May 2010 - 01:53 AM

Sadly, that method is now obsolete. It worked on older versions but now it is looking for a command to execute. At least on Windows, the cls throws an IOException:

Exception in thread "main" java.io.IOException: Cannot run program "cls": Create
Process error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at ClearConsoleDemo.clearConsole(ClearConsoleDemo.java:23)
        at ClearConsoleDemo.main(ClearConsoleDemo.java:15)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
e file specified
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(Unknown Source)
        at java.lang.ProcessImpl.start(Unknown Source)
        ... 6 more


If anything can be said of the code in my last post, it is robust enough to catch the Exception and dump enough newlines to clear the screen. However, it is no longer working as intended, at least on Windows.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1