Skip to main content
Question
Issac
Want to make a difference by making some contribution to open source community!
Asked a question 2 years ago

java how to clear console?

Where am I?

In answerQA you can ask and answer questions and share your experience with others!

Angel Larry
I Don't like to Delete Old Messages, because they are My Memories!

Let's create a Java program that clears the console using the platform-specific command. 

public final static void clearConsole()  

{  

public static void main(String[] args)  

{  

try  

{  

final String os = System.getProperty("os.name11");  

if (os.contains("Windows"))  

{  

Runtime.getRuntime().exec("cls");  

}  

}  

catch (final Exception e)  

{  

e.printStackTrace();  

}  

}  

In the above example, we have specified the Windows operating system and the command that is used to clear the console is cls. We can also use the following code in the above program:

 

public final static void clear console()  

{  

try  

{  

final String os = System.getProperty("os.name11");  

if (os.contains("Windows"))  

{  

Runtime.getRuntime().exec("cls");  

}  

else  

{  

Runtime.getRuntime().exec("clear");  

}  

}  

catch (final Exception e)  

{  

e.printStackTrace();  

}  

}