5

I want to clear screen in my java application, after reading many questions and googling, I found the below code

runtime.getruntime().exec("cls")

or

Runtime.getRuntime().exec("cmd /c cls");

but the above code doesn't work in windows 7. I am aware the "cls" script is domain specific, does anyone know what is the text I should use in windows 7. it will be really helpful, thank you in advance.

Radan
  • 1,630
  • 5
  • 25
  • 38

2 Answers2

2

Since cls is an internal command (something cmd.exe does itself rather than calling an executable program), you can do it with:

cmd /c cls

This works fine under Windows 7, assuming you're actually running a console-type application.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • am gonna try this, but it looks like one of the many combinations I tried before. – Radan Feb 21 '13 at 13:37
  • Runtime.getRuntime().exec("cmd /c cls"); dint work got IO exception as usual. – Radan Feb 21 '13 at 17:11
  • 1
    @Radan You need to pass the parameters as a string array, see this: http://stackoverflow.com/questions/3608944/adding-parameters-to-runtime-getruntime – lbalazscs Feb 22 '13 at 15:08
  • 1
    it still doesn't work here with: ```String[] command = new String[] {"cmd.exe", "/c", "cls"}; Runtime.getRuntime().exec(command);``` – Redoman Apr 24 '14 at 21:36
  • I can confirm that this didn't work for me either on Win7. I also tried: `String[] command = new String[] {"C:\\Windows\\System32\\cmd.exe", "/c", "cls"}; Runtime.getRuntime().exec(command);` There is no error given, the line just looks like it's skipped. – localhost Feb 09 '15 at 11:53
  • @localhost: it doesn’t work, even when there are no errors, because when running sub-processes this way, the output of of the command gets redirected. See [here](http://stackoverflow.com/a/33379766/2711488) – Holger Oct 28 '15 at 09:46
1

I realize you are looking for an easy way to clear the screen. You will have to use the newline hack or use an ANSI enabled console. Here is a little more difficult windows only method using JNA you or others reading this can consider. This is an instructional example. Add error checking/handling/imports/includes as necessary. You must already know how to use JNA. If you are new to JNA, this is a good 1st program for you to try.

//------------------------------------------
// Java2Win.class
//------------------------------------------
public interface Java2Win extends Library {
    Java2Win java2Win = (Java2Win)Native.loadLibrary("Java2Win64",Java2Win.class);
    void cls();
}
//------------------------------------------

//------------------------------------------
// Java2Win.c (Java2Win.dll & Java2Win64.dll)
//------------------------------------------
JNIEXPORT void cls() {
   system("cls");
}
//------------------------------------------

//------------------------------------------
// Test
//------------------------------------------
public static void main(final String args[]) throws Exception {
    final File file = new File("rootToDLL", "Java2Win64.dll");
    LibraryLoader.loadLibrary(file);
    System.out.println("-----some output");
    System.out.println("-----some output");
    System.out.println("-----some output");
    Thread.sleep(2000);
    Java2Win.java2Win.cls();
    System.out.println("-----cleared");
}
//------------------------------------------
Java42
  • 7,628
  • 1
  • 32
  • 50