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?
Asked
Active
Viewed 8,533 times
4 Answers
1
System.out.println("\033[2J\n");
This shall do it :)

Abhishek
- 874
- 2
- 8
- 23
-
Doesn't work for me. Windows 7, Java 7 – MadProgrammer Aug 14 '13 at 06:39
-
whats your compiler..blue J? – Abhishek Aug 14 '13 at 07:11
-
if its blueJ then try System.out.println("\f"); – Abhishek Aug 14 '13 at 07:12
-
Plain on Java 7, tried `System.out.println("\f");` from another answer, didn't work :P – MadProgrammer Aug 14 '13 at 07:22
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)
{
}
}

Jose Antonio Benitez Montero
- 1,846
- 2
- 19
- 25
-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
-
3*"Cannot run program "cls": CreateProcess error=2, The system cannot find the file specified"* - Windows 7, Java 7 – MadProgrammer Aug 14 '13 at 06:41
-