How to Run CLS From Java

The "CLS" command clears the screen in the Microsoft Windows command line. You can use Java to interface with the Windows command using the "Runtime" function. Java opens the external application, runs the command and returns the output to a Java variable. The Java variable is used to determine if an error was returned by Windows, or if the command was successful.

Instructions

    • 1

      Right-click the Java file you want to edit and select "Open With." Click the Java compiler you want to use to edit your code.

    • 2

      Create the commands to call the Windows command line and run "CLS" in the prompt. The following code shows you how to set up the commands:

      List<String> runcommand = new ArrayList<String>();
      runcommand.add("cmd.exe");
      runcommand.add("CLS");

    • 3

      Execute the commands. The following code executes the command and returns the value to the "p" variable:

      Process p = Runtime.getRuntime().exec(runcommand);

Related Searches:

References

Comments

Related Ads

Featured