Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+12 votes
5 views
in Java by (920 points)
edited by
I have output in my Java console from running an application, how can I clear it using code?

5 Answers

+13 votes
by (13.2k points)
edited by

In java, there is a function to clear console and not a command ,like in C++ there is a command :

system("CLS");

In java, there is a function :

public static void clearScreen() 

{     

System.out.print("\033[H\033[2J");  

 System.out.flush();  

}

+1 vote
by (32.3k points)
edited by

Create a method in your class like this:

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) {}

}

I haven’t checked this for Linux But this works in Windows.

Want to learn Java end-to-end? Here's an informative video on Java:

Also, there is an alternate method to write this code in clrscr():

for(int i = 0; i < 80*300; i++) // Default Height of cmd is 300 and Default width is 80

    System.out.print("\b"); // Prints a backspace

0 votes
by
edited by
System.out.print("\f"); 

Paste the above code in your editor to clear the terminal screen when the program is running

Below is a sample code: 

public class ClearScreen
{
    public static void main(String args[])
    {
        for(int i = 1 ; i < 4 ; i++)
        {
            System.out.println("This code will get erased after it is printed " + (4-i) + " more time(s)"); 
            try
            {
                Thread.sleep(2000);
            }
            catch(Exception e)
            {    
            }
        }
        // code to clear screen 
        System.out.print("\f");    
        // printing after erased
        System.out.println("Code was erased!!");
    }
}
0 votes
by (37.3k points)

You can use the following function to clear the command line console: 

For Windows:

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) {} 

}

For Linux:

System.out.print("\033\143");

0 votes
ago by (1.1k points)

In a Java program, clearing the terminal or console screen is possible using ANSI escape codes provided the terminal is able to support it:

public class ClearConsole {

public static void main(String[] args) {

clearConsole();

System.out.println("Console cleared!");

}

public static void clearConsole() {

System.out.print("\033[H\033[2J");

System.out.flush();

}

}

Note:

This is only applicable if the console in the windows is a UNIX system like Linux or MAC. In the Windows command prompt, it may not work in the proper manner. If an answer is required that can work in several operating systems, doing the following would suffice: print several new lines.

public static void clearConsole() {

for (int i = 0; i < 50; ++i) System.out.println();

}

This approach, however, does not really clear the output buffer, but rather gives the impression of clearing the console.

Related questions

0 votes
1 answer
asked Jul 13, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...