How to clear the console in Java

0 votes
I want to clear the console. How can I do this?
May 22, 2018 in Java by Daisy
• 8,140 points
35,982 views

3 answers to this question.

0 votes
public static void clearScreen() {  
    System.out.print("\033[H\033[2J");  
    System.out.flush();  
}  

The code given above will help you to clear the screen.

Hope it helps!!

If you want to be the good Java programmer, then it is suggested to join comprehensive Java online course today.

answered May 22, 2018 by Parth
• 4,640 points
it not works, am i wrong?
Hey,

Are you getting any error in doing this? If you are getting please post it here, it will be helpful to investigate further.
not worked just printing this on the console window "\033[H\033[2J".

Hey, @There,

I would suggest you try this: 

Runtime.getRuntime().exec("cls");
0 votes
import java.io.IOException;

public class CLS {
    public static void main(String... arg) throws IOException, InterruptedException {
        new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
    }
}
Now when the Java process is connected to a console, i.e. has been started from a command line without output redirection, it will clear the console.
answered Aug 27, 2018 by Sushmita
• 6,920 points
0 votes
import java.io.IOException;

public class chkClearScreen {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        clear();
        System.out.println("Screen out has been flushed");
    }

    public static void clear()
    {
        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) {}
    }
}
answered Aug 26, 2020 by ItsJustRaja

Related Questions In Java

0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
799 views
0 votes
2 answers

How to break the nested loop in Java?

You can use break with a label for the ...READ MORE

answered Sep 20, 2018 in Java by Sushmita
• 6,920 points
1,184 views
0 votes
1 answer

How to read text files from the Classpath in Java?

InputStream in = this.getClass().getClassLoader().getResourceAsStream("TextFile.txt"); InputStream in = this.getClass().getResourceAsStream("/TextFile.txt"); package ...READ MORE

answered May 8, 2018 in Java by Akrati
• 3,190 points
2,778 views
0 votes
1 answer

How to pad an integer with zeros on the left in Java?

Use java.lang.String.format() method. String.format("%05d", number ...READ MORE

answered May 31, 2018 in Java by Daisy
• 8,140 points
2,218 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,253 views
0 votes
1 answer

What are optional parameters in Java

Using three dots: public void move(Object... x) { ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,350 points
846 views
0 votes
1 answer

How to split Strings by space in Java ?

You can use split() method. str = "Hello ...READ MORE

answered May 16, 2018 in Java by sharth
• 3,370 points
1,493 views
0 votes
2 answers

How can we add leading zeros to the number in Java?

From Java 1.5 you can use the String.format method. ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 59,230 points
5,081 views
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by Parth
• 4,640 points
913 views
0 votes
1 answer

How to stop the Thread in Java?

Use a flag, which will indicate the ...READ MORE

answered Jun 11, 2018 in Java by Parth
• 4,640 points
1,502 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP