. Advertisement .
..3..
. Advertisement .
..4..
Do you know how to clear the console in Java? If you answer this question with a reluctant shake of the head, this article is a perfect fit for you. Our guideline will give you detailed instructions and examples of two plausible methods. Assess each of them to select the most suitable one for your Java programming!
How to Clear The Console in Java?
Method 1: Use The ANSI Escapade Codes
The first plausible approach is to use ANSI codes. They are special escaping codes whose sequences shift the cursor places or show varied hues. You may interpret those sequences as commands that combine characters and bytes.
So how to use it to clear the console in Java? To do so, you may employ the escapade code \033[H\033[2J. This somewhat unusual character set symbolizes the order for console cleaning. Let’s break this function down to grasp it better.
First, \033 refers to the ESC (or the escapade character). When fusing [H with 033, the cursor can move to a particular place. On the other hand, the characters 033[2J will clean up your whole screen.
Let’s look at the illustration below, which employs the escapade codes. It also applies System.out.flush(), a function tailored for flushing out the rest of your bytes that linger from System.out.print(). That way, the system will leave nothing behind on your console screen.
public class ClearConsoleScreen {
public static void main(String[] args){
System.out.print("Everything on the console will cleared");
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
Method 2: Use The System.getProperty ()
It is also plausible to employ a command based on our utilized platform. What does this strategy entail exactly? First, you may obtain the system property via a getProperty() function from the System category. Once done, choose the command operating in your platform. That tactic will help clear the console in Java!
One of such commands is the System.getProperty(), a static approach for the System category. Programmers often use it to yield the systemic property inferred by a particular key. This method parses a key from the String type; it serves as a parameter and will give a clear name for that property before sending it back later on.
Here is a basic syntax:
public static String getProperty(String key)
However, there are some exceptions to keep in your mind:
SecurityException: It will occur if you have some security managers. Also, the checkPropertyAccess() function will not allow entry to a particular system property.
NullPointerException: What happens when you fail to specify your key null? The system will throw an exception.
IllegalArgumentException: This exception will take place if your key has no content.
Now you have grasped the fundamentals of this command. Let’s look at the example for further comprehension!
public class ClearScreenExample2
{
public final static void clearConsole()
{
public static void main(String[] args)
{
try
{
final String os = System.getProperty("os.name");
if (os.contains("Windows"))
{
Runtime.getRuntime().exec("cls");
}
}
catch (final Exception e)
{
e.printStackTrace();
}
}
Sometimes, there might be a “command not found: jest” glitches, which often occur during command-operated tasks. You may refer to this guideline for further instructions on tackling this error.
Conclusion
This article has shown you how to clear the console in Java in the most time-efficient and straightforward method. ITtutoria believes these intensive guidelines might be of help to you. Feel free to reach out if certain aspects still trouble you!
Leave a comment