1

I saw this topic: Java: Clear the console but does not work in Windows for me.

I have this code:

import java.io.IOException;

public class hola {
public static void limpiar_pantalla() throws IOException {
        String os = System.getProperty("os.name");

        if (os.contains("Windows"))
        {
            System.out.println("Win 1");System.out.println("Win 1");
            Runtime.getRuntime().exec("cls");
            //for (int i = 0; i < 50; ++i) System.out.println();
            System.out.println("Win 2");
        }
        else
        {
            System.out.println("Linux 1");
            Runtime.getRuntime().exec("clear");
            //for (int i = 0; i < 50; ++i) System.out.println();
            System.out.println("Linux 2");
        }

}
public static void main(String[] args) throws IOException {
    hola.limpiar_pantalla();
    }

}

This is the Linux output:

Linux 1

Linux 2

and the windows output:

F:\bin>java -jar sacar_systema_java.jar Win 1 Exception in thread "main" java.io.IOException: Cannot run program "cls": Create

Process error=2, El sistema no puede hallar el archivo especificado

    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at hola.limpiar_pantalla(hola.java:10)
    at hola.main(hola.java:24)

Caused by: java.io.IOException: CreateProcess error=2, El sistema no puede hallar el archivo especificado

    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 6 more

F:\bin>

basicly cls: command not found

I don' know why does not work.

Can anybody help?

braX
  • 11,506
  • 5
  • 20
  • 33
RobertPro
  • 172
  • 1
  • 13

1 Answers1

1

This method uses the command line tool "cls" to clear the console and not a java function. It looks like your local machine simply doesn't have the "cls" command you want to use. You can try to use the command line in windows to call cls there. It will probably fail with the same error message.

luz
  • 113
  • 2
  • 7