Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Clearing the console in Java

    I'm going mad everyone.

    All I want to do is clear the console's buffer (i.e. "cls in Windows, "clear" in Unix).

    I've googled this quite a bit and found nothing useful.

    Sun's forums are completely useless, there are about 20 threads asking the same question and the only replies are to put in a loop with a bunch of System.out.println(). There is another "solution" but it's a nasty hack that involes editing system files (and thus makes it difficult to distribute my application!).

    I'm just not convinced that there's "no way to do it."

    Does ANYONE know how to clear the console??!

    Thanks,
    Kyle

    Edit: Also, I've tried using Runtime.exec() to execute the "cls" command, but that seems to start a new console session.
    Last edited by dcoder; 09-15-2005 at 04:34 PM.

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    There is a way -- by outputting 80 empty lines.

    Java does not assume standard out is a console. Therefore, it has no way to clear it. Java is about cross-platform applications.
    If you want to clear the console, you have to be making an assumption there IS one. Therefore, it is no longer cross-platform.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heh. Again, I refuse to print a bunch of empty lines. There's got to be a more elegant way to do it!

    And just because Java doesn't assume standard out is a console doesn't mean there isn't a way to clear it. Java can write to the console without having to assume standard out is a console, so why should it have to assume this in order to clear it?

    Maybe I'm just being to stubborn with this one ...I may have to email Sun.

  • #4
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Alright, I admit defeat. I just followed the heirarchy upwards from the PrintStream 'out' in System, and it looks like nowhere in this heirarchy is a formal method for clearing the OutputStream defined.



    I guess I'll have to find another way to write text that can easily be cleared...

  • #5
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Well, why are you using the console for this, then?
    Use a real GUI (swing, etc) and you can clear whatever you want...

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •