0

Can we clear the cmd screen while making console application with java code.In c or c++ we use clrscr() method but in what code we need to do it in java?

Shikhil Bhalla
  • 392
  • 1
  • 5
  • 17

4 Answers4

1
System.out.println("\033[2J\n");

This shall do it :)

Abhishek
  • 874
  • 2
  • 8
  • 23
0

Printing out the form-feed character will clear the screen

System.out.print("\f");

Hope this helps!

Anurag
  • 137
  • 7
-2

You can use this code

private static void Clear()
{
    try
    {
        String os = System.getProperty("os.name");

        if (os.contains("Windows"))
        {
            Runtime.getRuntime().exec("cls");
        }
        else
        {
            Runtime.getRuntime().exec("clear");
        }
    }
    catch (Exception exception)
    {
    }
}
-3

Java does not provide any such functionality. Output on terminal is just a special case of redirecting standard output(standard input or standard error). As such output stream has no knowledge what the stream is redirected to.

You can use hacks to achieve your goal though. Refer this question. Best way I thing is the following

    String os = System.getProperty("os.name");

    if (os.contains("Windows"))
    {
        Runtime.getRuntime().exec("cls");
    }
    else
    {
        Runtime.getRuntime().exec("clear");
    }
Community
  • 1
  • 1
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289