3

I am working on a game that involves clearing the screen after each turn for readability. The only problem is I cannot use the Windows command prompt-based "cls" command and it does not support ANSI escape characters. I used Dyndrilliac's solution on the following page but it resulted in an IOException:

Java: Clear the console

Replacing "cls" with "cmd \C cls" only opened a new command prompt, cleared it, and closed it without accessing the current console. How do I make a Java program running through Windows Command Prompt access the command prompt's arguments and use them to clear its output?

Community
  • 1
  • 1
greatmastermario
  • 87
  • 1
  • 1
  • 11
  • In C, `system("cls");` works for me. But I also get an exception when I try the Java solution; I think the reason is that there's no executable named `cls.exe`. So that means `system()` must be doing something special, but I don't know what. It might require digging into the C library sources to figure out how it works. – ajb Oct 08 '13 at 16:08
  • 1
    By the way, I don't understand why you're referring to the "command prompt's arguments". How does that relate to clearing the console? – ajb Oct 08 '13 at 16:14
  • @ajb CLS is actually a Windows Command Prompt argument, not its own process. It is meant to clear the Windows Console when used, but does not execute any certain program. Therefore, doing "Runtime.getRuntime().exec("cls") actually tries to execute a program called "cls" instead of sending the argument to the console. – greatmastermario Oct 08 '13 at 16:23
  • "cls" is a Windows Command Prompt **command**, not an argument. Arguments are the extra information that you give to commands or programs; if you run `java JavaProgram file1 file2`, then `file1` and `file2` are the arguments (and will be the `String[]` that the main program gets as arguments). The mistake in terminology left me confused. – ajb Oct 08 '13 at 16:33
  • Still, the error occurs because it is trying to run a process when you use Runtime.getRuntime().exec("cls"). – greatmastermario Oct 08 '13 at 16:48
  • @greatmastermario have you found the solution? – Baby Dec 19 '13 at 01:05

4 Answers4

11
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();

Solved here: Java: Clear the console

I realize this is an old post, but I hate when I find questions with responses of never mind i got it, or it just dies off. Hopefully it helps someone as it did for me.

Keep in mind it won't work in eclipse, but will in the regular console. take it a step further with if you're worried about cross OS:

        final String os = System.getProperty("os.name");
        if (os.contains("Windows"))
            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
        else
            Runtime.getRuntime().exec("clear");
Community
  • 1
  • 1
Sean
  • 126
  • 1
  • 4
  • I would like to assume that using this requires wrapping it in a method, correct? – greatmastermario Nov 06 '15 at 01:22
  • Im glad this worked for you. No wrapper class needed, I threw it in a sub by itself. Keep in mind it doesnt work in the eclipse console. It will work in the command prompt though. Eclipse doesnt recognise anything that clear screens. They said it was relevant to app security. Some stated it was fixed in mars but I couldn't get it to work. – Sean Nov 07 '15 at 01:43
  • Doesn't work in the NetBeans 8 console, but works great in the DOS Prompt in Windows 7. – james.garriss Feb 11 '16 at 13:56
1
public static void clrscr(){
//Clears Screen in java
try {
    if (System.getProperty("os.name").contains("Windows"))
        new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
    else
        Runtime.getRuntime().exec("clear");
} catch (IOException | InterruptedException ex) {}
}
-2

There's pretty much nothing in the console related API to do a clear screen. But, you can achieve the same effect through println()s. A lot of putty clients clear the page like that and then scroll up.

private static final int PAGE_SIZE = 25;

public static void main(String[] args) {
    // ...
    clearScreen();
}

private static void clearScreen() {
    for (int i = 0; i < PAGE_SIZE; i++) {
        System.out.println();
    }
}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • 1
    I know that works, but the problem is I want the cursor to be at the top of the console after clearing it. The "cls" command works with Python, and I have found other solutions to this, but they don't work on my computer. I just need to use arguments in the current Windows console running the application. – greatmastermario Oct 08 '13 at 16:13
-2

Create a batch file to clear the cmd screen and run your java program

Step 1. Create a file with extension .bat Step 2. So your code in batch file will be

Cls Cd desktop // path Javac filename.java // compiling Java desk // running

By doing this....you can clear the screen during run time

  • So what exactly does that do, so I can implement it correctly? In other words, what does each part mean and how do I use it in my program? – greatmastermario Mar 19 '14 at 02:46
  • so wen u run that file using cmd....it will clear the console screen and runs the java program at the same time –  Apr 06 '14 at 12:45
  • Why would you create a batch script just to clear the screen? This seems wholly unnecessary. – CATboardBETA Apr 20 '21 at 17:32