Implement the same function as C, C ++ system ("cls"); in Java


I made a console Othello game in C a while ago, so When I tried to make it in Java I wondered if system ("cls"); made in C could be done in Java, but I've had a lot of trouble, so I'll make a note here.

If you google

There seem to be several ways, There were a lot of confusing things like errors and warnings. Among them, I will introduce the one that was able to be implemented most smoothly this time.

This time, in making Othello I also wanted to consider extensibility when I wanted to control the output of the screen, so Create a ConsoleControl class that controls the output of the console screen It was implemented as a clearScreen method that clears the screen output.

Sorce Code ##


 *Class that controls the output of the console screen
public class ConsoleControl {
	 *Method to clear the output of the console screen
	 * @throws	IOException
	 * @throws 	InterruptedException
	public void clearScreen() throws IOException, InterruptedException {
        new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();

For the time being, by calling the clearScreen method in the main method, I was able to clear the character string displayed at the command prompt.


I still couldn't understand why it would be "cls" if I wrote it like this because of my lack of knowledge. After studying and catching up with my understanding, I would like to add that amount.

Later talk

2018/04/26 postscript When I visited after a long time, the number of views was close to 2000 views instead of 1000 views, so While swallowing it as a live broadcast material, I analyzed this program and investigated what kind of processing it was doing. Kusso, roughly smart.

ProccessBuilder class

First of all, regarding this ProccessBuilder class, if you look at the official explanation, By saying "class for building an instance of Process class" By creating an instance of this class, you can build an execution environment for a new process using external commands. Looking at the contents with Open JDK SE8, The command specified in the argument of the constructor is in the ProcessBuilder class It was stored in a List type variable called command. Below is a source quote from Open JDK

    private List<String> command;
    private File directory;
    private Map<String,String> environment;
    private boolean redirectErrorStream;
    private Redirect[] redirects;

    public ProcessBuilder(String... command) {
        this.command = new ArrayList<>(command.length);
        for (String arg : command)

inheritIO method

This method was defined as follows: (Source quote from Open JDK)

    public ProcessBuilder inheritIO() {
        Arrays.fill(redirects(), Redirect.INHERIT);
        return this;

This method just assigns the INHERIT flag to redirects using the Arrays.fill function. What does this INHERIT flag mean? The official description says:

This is the normal behavior of most operating system command interpreters (shells).

Also, the explanation about the inheritIO method is

Set the standard input / output source and output destination of the subprocess to be the same as the current Java process. This is a simple method. The following form of call pb.inheritIO() It behaves exactly the same as the next call. pb.redirectInput(Redirect.INHERIT) .redirectOutput(Redirect.INHERIT) .redirectError(Redirect.INHERIT) This behavior is equivalent to most operating system command interpreters and the standard C library function system ().

In other words, it turned out that the input / output of external programs can be integrated with the standard input / output of Java. See here for standard I / O. Well, at the end of the official explanation

This behavior is equivalent to most operating system command interpreters and standard C library functions system ().

If you think that it works the same as system (), you can explain the mandokusai by poi.

start method

The source is long, so it is omitted. This method starts the command or executable file specified by ProcessBuilder. Somehow you can guess from the method name.

waitFor method

The source is (ry This method makes the thread currently being processed wait until the subprocess running in another thread ends.


I modified the previous source to do the following:


public class ConsoleControl {
	private ProcessBuilder pb;

	 *The constructor for the ConsoleControl class.
	 *Builds an environment to execute a new process that executes the specified command.
	 * @param command Command to execute
	public ConsoleControl(String... command) {
		pb = new ProcessBuilder(command);

	 *A method that clears the command prompt screen.
	public void cls() throws IOException, InterruptedException {
		 * //External command specified in the constructor argument of ProcesserBuild
		 * //Converted for execution at the command prompt
		 * ProcessBuilder pbInheritIO = pb.inheritIO();
		 * //Execute with external command
		 * Process pro = pbInheritIO.start();
		 * //Wait until the process running on another thread is finished
		 * pro.waitFor();

For Windows

var cc = new ConsoleControl("cmd", "/c", "cls");

For Linux

var cc = new ConsoleControl("/bin/bash", "-c", "clear");

Is it like that? Perhaps the explanation is insufficient or misunderstood, so I would be grateful if you could point it out. Also, if you have free time, I'd be happy if you could come visit us for live broadcasting.


Recommended Posts

Implement the same function as C, C ++ system ("cls"); in Java
Implement PHP implode function in Java
[Java] Implement a function that uses a class implemented in the Builder pattern
[Java] Something is displayed as "-0.0" in the output
Import files of the same hierarchy in Java
Parse the date and time string formatted by the C asctime function in Java
I tried to implement the Euclidean algorithm in Java
Differences in code when using the length system in Java
Java desktop with the same front end as the WEB.
Implement application function in Rails
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
Implement follow function in Rails
Refer to C ++ in the Android Studio module (Java / kotlin)
Implement two-step verification in Java
Fighting notes when trying to do the same thing as Java's XOR Mode in C #
Implement math combinations in Java
2 Implement simple parsing in Java
Implement Email Sending in Java
Implement functional quicksort in Java
Reproduce Java enum in C #
Implement rm -rf in Java.
Implement XML signature in Java
How to use the function implemented in Kotlin Interface introduced in Maven as the default implementation from Java 8
Examine the system information of AWS Lambda operating environment in Java
Defeat the hassle of treating C arrays as Tuples in Swift
[Behavior confirmed in December 2020] How to implement the alert display function
Rails' Private methods hurt if you feel the same as Java!
Implement star evaluation function in Rails 6 (Webpacker is installed as standard)
Determine if the strings to be compared are the same in Java
How to use MinIO with the same function as S3 Use docker-compose
Access the network interface in Java
Implement Table Driven Test in Java 14
Guess the character code in Java
3 Implement a simple interpreter in Java
Read Java properties file in C #
Specify the java location in eclipse.ini
Java, JavaScript, C # (difference in assignment)
Unzip the zip file in Java
Implement tagging function in form object
Implement reCAPTCHA v3 in Java / Spring
Parsing the COTOHA API in Java
Try to implement Yubaba in Java
Implement CSV download function in Rails
1 Implement simple lexical analysis in Java
Implementation of like function in Java
Call the super method in Java
Note: Differences from Java as seen from C #
Get the result of POST in Java
How to implement date calculation in Java
How to implement Kalman filter in Java
Implement API Gateway Lambda Authorizer in Java Lambda
Java reference to understand in the figure
[Java] How to use the hasNext function
Try to implement n-ary addition in Java
Try using the Stream API in Java
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
I tried the new era in Java
Output Notes document as XML document in Java
[Java] Use cryptography in the standard library
Organized memo in the head (Java --Array)
Try calling the CORBA service in Java 11+