Click to See Complete Forum and Search --> : Java: How to clear command line? Quick help plz...
adamxyz
10-02-2001, 03:08 PM
I'm making a program for one of my cs classes, that won't use the graphic interface. Instead it will "system.out.println()" a lot of text.
My question is: How do you clear the command line, like 'cls', in Java?
Thanks in advance!
Edit: That would be 'console' instead of 'command line' I suppose.
[This message has been edited by adamxyz (edited October 02, 2001).]
adamxyz
10-02-2001, 04:10 PM
Found this one: http://www.rgagnon.com/javadetails/java-0047.html
but that won't do, since I'm on both PC and Sun. Need something cross-platform...
driver
10-02-2001, 06:36 PM
If your printing a lot of text to the console, you may want to consider just putting it into a file. Writing to a file in Java is more involved than in C++, but still pretty easy.
ie
PrintWriter fOut;
fOut=new PrintWriter(new BufferedWriter(new FileWriter("fileName")));
fOut.println("what you want to print");
fOut.close();
bryce777
10-03-2001, 01:11 AM
I'm fairly sure that to do a real cls would be a system call, and so would not be cross-platform compatible. I didn't check the link but I am guessing they called exec() at some point.
You could always print a bunch of newlines, I guess, bt maybe a file would be better.
------------------
system specs:
Voodoo 5 5500 agp
tyan 1834d tiger 133 dual 800eb 133mhz FSB
via chipset 133 via apollo pro (don't make this mistake)
256 MB RAM
2 maxtor 60gig ata100 drives
promise ata100 controller
liteon 52 truex cdrom
Linksys ethernet 10/100
Soundblaster Live! (what's so exciting about it??) value edition
300watt power supply (inwin)
about 7 pounds of fans (I'm not kidding)
Suse 7.1(god gnome is crappy compared to CDE)/win2000 based system
adamxyz
10-03-2001, 07:29 AM
Originally posted by driver:
If your printing a lot of text to the console, you may want to consider just putting it into a file. Writing to a file in Java is more involved than in C++, but still pretty easy.
ie
PrintWriter fOut;
fOut=new PrintWriter(new BufferedWriter(new FileWriter("fileName")));
fOut.println("what you want to print");
fOut.close();
How would printing the text to a file help me? I don't get it...
The thing is, I want a console-based menu. After stepping through the menus for a while, the console will get "long", that's what I want to avoid.
By printing the menu to a file first...what would I do with that? Even if I then output the file contents to the console, and clear the file after each output, the console will get cluttered.
Or is it just me... http://www.sharkyforums.com/ubb/smile.gif
driver
10-03-2001, 11:58 AM
I didn't realize you were creating a meu system, I was under the impression that you were just outputting a lot of information for the user to read, not neccessarily to interact with. My bad. Well, you may want to try and write a GUI interface for your program. It's pretty easy in Java and I believe it allows you to clear the display.
adamxyz
10-03-2001, 01:39 PM
Yeah, I think I will have to make a GUI, not that it's harder. It's just that my teacher somehow seems to like console output. I'll just make a console-like GUI then. http://www.sharkyforums.com/ubb/smile.gif
Thanks anyway!
alienrash
10-04-2001, 07:36 AM
You could query the OS and depending on the result execute the appropriate 'clear' command.
To make it completely portable, you could create a file that has an OS token and the command it should execute.
So, the file would look something like:
Windows NT 4.0=cls
Solaris=clear
SomeOtherOs=blahblah
Read the file in, parse it, and store it in a hashtable. Use System.getProperty("os.name") to get the name. Use this name to look up the command that you stored in the hashtable.
To execute the string command you need to use the Runnable and Process classes.
Good luck
------------------
Blueskies Blackdeath
adamxyz
10-12-2001, 12:28 PM
Originally posted by alienrash:
To execute the string command you need to use the Runnable and Process classes.
Hmm, how would I go about doing that?
I tried:
Runtime.getRuntime().exec("cmd /c cls");
but it didn't work. Does it perhaps run in a separate thread, which I don't know anything about, and therefore doesn't affect the current console that I'm outputting to. Starting a different program, like ...exec("notepad... works just fine, so there's obviously nothing wrong with the methods.
Another thing, what's the command.com or cmd equivalent for Solaris?
alienrash
10-12-2001, 07:19 PM
DOH! You are right. It is spawning another thread in which case your app will not be affected.
Originally posted by adamxyz:
Hmm, how would I go about doing that?
I tried:
Runtime.getRuntime().exec("cmd /c cls");
but it didn't work. Does it perhaps run in a separate thread, which I don't know anything about, and therefore doesn't affect the current console that I'm outputting to. Starting a different program, like ...exec("notepad... works just fine, so there's obviously nothing wrong with the methods.
Another thing, what's the command.com or cmd equivalent for Solaris?
------------------
Blueskies Blackdeath
adamxyz
10-12-2001, 07:30 PM
Originally posted by alienrash:
DOH!
Hehee. That's what I've been telling myself too. http://www.sharkyforums.com/ubb/wink.gif