How To Clear Screen In Java
hello all
how can i clear the DOS screen through java program
i hava search so many web site but i can't found the proper ans.
thanx in advance [:)]
- 10 Contributors
- forum 22 Replies
- 12,080 Views
- 2 Years Discussion Span
- comment Latest Post Latest Post by stultuske
Recommended Answers
Hello, I haven't faced such problem before .. maybe this might help you:
Java Programming [Archive] - Clear screen?
Jump to Post
The link, I gave you, contains the discussion, where your questions were explained .. also it gives one more example of clearing :)
Jump to Post
Here's a decent thread on the subject. Since you mentioned "DOS", I assume you really mean "Windows Console" since very few people use DOS now.
http://www.dreamincode.net/forums/showtopic9880.htm
I couldn't get it to …
Jump to Post
Sure .. here we go:
TheTestclass in this program represents your console window. It has some methods, but the one you're interested in is: cls().What actually it does in the main method:
1. Creates an instance of console window
2. Prints some text in there
3. …
Jump to Post
Method
Runtime.getRuntime().exec()requires an external command. CLS is internal dos command.
To run internal command using exec() we have to use CMD (external) command.
Try it at command prompt.>cmd /? -- help >cmd /c cls// Will create a new process Process ps=Runtime.getRuntime().exec (new …
Jump to Post
All 22 Replies
thanx for ur reply
but i have already used this code
char esc = 27;
String clear = esc + "[2J";
System.out.print(clear);
And this line must be inside the config.sys:
device=c:\windows\command\ansi.sys
" And this line must be inside the config.sys:
device=c:\windows\command\ansi.sys "
means what ?? can u explain me ???
what should i do ??
Hello, I haven't faced such problem before .. maybe this might help you:
Java Programming [Archive] - Clear screen?
The link, I gave you, contains the discussion, where your questions were explained .. also it gives one more example of clearing :)
Sorry but i can't understand what the program "TEST" is abt ??
can u help me ??
The link, I gave you, contains the discussion, where your questions were explained .. also it gives one more example of clearing :)
VernonDozier 2,218 Posting Expert Featured Poster
Here's a decent thread on the subject. Since you mentioned "DOS", I assume you really mean "Windows Console" since very few people use DOS now.
http://www.dreamincode.net/forums/showtopic9880.htm
I couldn't get it to work for me, but I didn't play with it for all that long. It looked like it worked for other people though. Basically you are trying to execute the "cls" command through Runtime.getRuntime().exec or something very similar.
Hope this helps.
Sure .. here we go:
The Test class in this program represents your console window. It has some methods, but the one you're interested in is: cls().
What actually it does in the main method:
1. Creates an instance of console window
2. Prints some text in there
3. Waits for 5 seconds
4. Clears the window
5. Waiting for 5 seconds one more time
6. Exit
The waiting thing there ( Thread.sleep(5000); ) made only for demonstrative purposes.
Ask if you have any other questions :)
hi!!!
i have tried these code
but it does n't work (do not give any error)
1)
String clearScreenCommand = null; if( System.getProperty( "os.name" ).startsWith( "Window" ) ) clearScreenCommand = "cls"; else clearScreenCommand = "clear"; Runtime.getRuntime().exec( clearScreenCommand ); 2)
Runtime.getRuntime().exec("cls"); 3) (provide the error )
try { Runtime.getRuntime().exec("cls"); } catch (IOException e) { e.printStackTrace(); } 4)
System.Console.Clear(); (provide the error )
cannot find symbol
symbol : variable Console
location: class java.lang.System
System.Console.Clear(); ^ whould u explian me through simple program ?
sholud i have add any pakage ???
Here's a decent thread on the subject. Since you mentioned "DOS", I assume you really mean "Windows Console" since very few people use DOS now.
http://www.dreamincode.net/forums/showtopic9880.htm
I couldn't get it to work for me, but I didn't play with it for all that long. It looked like it worked for other people though. Basically you are trying to execute the "cls" command through Runtime.getRuntime().exec or something very similar.
Hope this helps.
Edited by Reverend Jim because: Fixed formatting
VernonDozier 2,218 Posting Expert Featured Poster
I didn't get it to work for me, so I can't help you debug much. Regarding console, import java.io.Console . Case counts.
import java.io.Console; public class ClearScreen { public static void main (String args[]) { Console cons = System.console (); } } I see no Clear () command in the Console class though, so i don't know where you got that.
http://java.sun.com/javase/6/docs/api/java/io/Console.html
I think this may be a dead end.
I think the answer must be Runtime.getRuntime().exec (....) , though when I run it, it says it can't find the program "cls", so there's something more to it. Wish I could be of more help.
__avd 1,826 Posting Genius (adatapost) Team Colleague
Method
Runtime.getRuntime().exec() requires an external command. CLS is internal dos command.
To run internal command using exec() we have to use CMD (external) command.
Try it at command prompt.
>cmd /? -- help >cmd /c cls // Will create a new process Process ps=Runtime.getRuntime().exec (new String[]{"cmd","/c","cls"}); ps.waitFor();
VernonDozier 2,218 Posting Expert Featured Poster
Method
Runtime.getRuntime().exec()requires an external command. CLS is internal dos command.
To run internal command using exec() we have to use CMD (external) command.
Try it at command prompt.>cmd /? -- help >cmd /c cls// Will create a new process Process ps=Runtime.getRuntime().exec (new String[]{"cmd","/c","cls"}); ps.waitFor();
Well, here's the program I wrote. The screen isn't cleared. Anything wrong?
Both display strings are displayed and no exceptions are displayed.
public class ClearScreen { public static void main (String args[]) { try { // Will create a new process Process ps=Runtime.getRuntime().exec (new String[]{"cmd","/c","cls"}); ps.waitFor(); System.out.println ("After process waitFor : Screen should be clear now?"); } catch (Exception ex) { System.out.println (ex.toString ()); } System.out.println ("After catch block"); } }
__avd 1,826 Posting Genius (adatapost) Team Colleague
I am sorry. In my post: I said, you can't execute dos internal command directly because they are only interpreted by the shell (cmd command).
I apologize for the inconvenience.
PS: exec() method cannot read or write anything interactively. It has getInputStream(), and getOutputStream() methods.
@ adatapost
sorry but it's n't work !!!!
Method
Runtime.getRuntime().exec()requires an external command. CLS is internal dos command.
To run internal command using exec() we have to use CMD (external) command.
Try it at command prompt.>cmd /? -- help >cmd /c cls// Will create a new process Process ps=Runtime.getRuntime().exec (new String[]{"cmd","/c","cls"}); ps.waitFor();
Y'know, I've tried some of the methods that you guys mentioned, and they didn't do split!
Here's what I did:
Declarations:
Runtime runtime; Process process; int errno = 0; BufferedReader reader; After getting some input, I go to clear the screen:
runtime = Runtime.getRuntime(); // define the Process object and clear the screen. process = runtime.exec("cmd /c cls"); while ((line = reader.readLine()) != null) { System.out.println(line); } System.out.flush(); reader.close(); errno = process.waitFor(); System.out.println("\"cmd /c cls\" returned " + errno); The results were:
Enter something
blah"cmd /c cls" returned 0
What I couldn't show you in the above quote, is a little "square" that was returned by the output of the cmd utility.
I am sorry. In my post: I said, you can't execute dos internal command directly because they are only interpreted by the shell (cmd command).
I apologize for the inconvenience.
PS: exec() method cannot read or write anything interactively. It has getInputStream(), and getOutputStream() methods.
So, it would look like that the only way to clear the screen in java is to execute some sort of method in the PrintStream class. Am I right?
I looked it up the methods at (PrintStream Class Documentation (Java SE 6)) and it wasn't there. It seems that there isn't any way to clear the screen in Java. If my observation is wrong, then can someone please post the solution to this problem? It seems lazy for the Java people not to provide a means to clear the screen in their library when Microsoft has been providing this luxury to programmers for decades!
Edited by michinobu_zoned because: n/a
I tried all the above but no use.
one thing i didn't get is i don't know in which package the function Runtime.getRuntime().exec("cls"); is...???
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Why do you have to complicate things? The answer is simple.
System.out.print("\f");
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Why do you have to complicate things? The answer is simple.
System.out.print("\f");
And did you try that before you posted it?
And did you try that before you posted it?
I have used that for my java project that runs in dos.
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
It certainly doesn't work in a Win7 command window - it prints an ankh symbol.
Edited by Ezzaral because: n/a
__avd 1,826 Posting Genius (adatapost) Team Colleague
Use Jansi
Jansi is a small java library that allows you to use ANSI escape sequences to format your console output which works even on windows.
Steps:
1. Download the jansi-1.6.jar
2. Code to test jansi api.
import org.fusesource.jansi.*; public class Test { public static void main(String []args) { AnsiConsole.systemInstall(); Ansi ansi=Ansi.ansi(); System.out.println(ansi.eraseScreen()); AnsiConsole.out.println(ansi.bg(Ansi.Color.RED) + "Hello"); } } 3. Compile and run
>javac -cp .;jansi-1.6.jar Test.java
>java -cp .;jansi-1.6.jar Test
shre 0 Newbie Poster
System.out.print("\f");
works for me
there are diffrent commands recognigzed for different versions
Edited by __avd because: Email snipped.
stultuske 1,116 Posting Maven Featured Poster
shre: this thread hasn't been answred/ followed for almost a year, I think the OP (who asked the question over two years ago) by now has his/her answer or doesn't really care anymore.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.
How To Clear Screen In Java
Source: https://www.daniweb.com/programming/software-development/threads/204548/how-to-clear-the-screen-in-java
Posted by: borgespries1980.blogspot.com

0 Response to "How To Clear Screen In Java"
Post a Comment