1

I am used to clear the screen with C++ with absolutely no problems, but it seems like it's not that simple with Java.

I have tried both Runtime.getRuntime().exec("cls"); and

String[] cls = new String[] {"cmd.exe", "/c", "cls"};
Runtime.getRuntime().exec(cls); 

Which are the solutions I have found around the web (mostly here in StackOverflow), but they don't seem to be working on my computer...

The first try reports the following caught IOException:

java.io.IOException: Cannot run program "cls": CreateProcess error=2, System cannot find the file specified
dabadaba
  • 9,064
  • 21
  • 85
  • 155
  • Are you executing your Java application in a Windows console or in IDE console e.g. Eclipse, Netbeans, etc? – Luiggi Mendoza Feb 11 '14 at 15:07
  • No IDE, just console. – dabadaba Feb 11 '14 at 15:07
  • 2
    `cls` is a console feature I think. I don't think there exists a program on your hard drive called `cls`. – Martijn Courteaux Feb 11 '14 at 15:07
  • You can't clear the console in java, the only thing you can do is to print a lot of `\n` to make all previous text disappear – BackSlash Feb 11 '14 at 15:08
  • So why doesn't the second try work? – dabadaba Feb 11 '14 at 15:08
  • @BackSlash I have read that answer before, and I have also read that clearing the screen is indeed possible, the answers I found were proven successfull by the posters. – dabadaba Feb 11 '14 at 15:09
  • I think this is the answer to your question. It was previously asked on Stack Overflow: http://stackoverflow.com/questions/14924146/clear-text-printed-on-the-command-line-with-java?rq=1 – msmolcic Feb 11 '14 at 15:09
  • Then what about this http://stackoverflow.com/questions/2979383/java-clear-the-console – dabadaba Feb 11 '14 at 15:12
  • @msmolcic92 And that answer doesn't help me, it does not work. – dabadaba Feb 11 '14 at 15:14
  • You might try a Curses-like library for Java: http://stackoverflow.com/q/439799/10077 – Fred Larson Feb 11 '14 at 15:15
  • Sorry it did not help – msmolcic Feb 11 '14 at 15:16
  • @FredLarson I tried that one before and I does not work either. As you can see in the tag and in the comments I am using Windows 7 cmd, which does not support ANSI escape sequences as the answer to the post you linked suggests. http://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences – dabadaba Feb 11 '14 at 15:23
  • And it is starting to buggering me that everyone is saying that this is a "possible duplicate" when I said very clearly that I have gone through many posts about this in the web and in StackOverflow and none of the answers worked....... – dabadaba Feb 11 '14 at 15:24
  • @dabadaba: So you're telling me you tried ALL of the answers to ALL of the duplicate questions? If none of these worked, we're unlikely to do better by rehashing the question again. – Fred Larson Feb 11 '14 at 15:27
  • Well I found a lot of different answers to the same questions but none worked so I still have hope that there will be a different new answer which does help me. – dabadaba Feb 11 '14 at 15:29
  • @dabadaba on my machine (win xp) it's working. i think on your machine (win 7) should be another command. – 1ac0 Feb 11 '14 at 15:33
  • Another console command? I type cls in my console and it does work. – dabadaba Feb 11 '14 at 15:38
  • @dabadaba There is no _reliable_ way to do it in ***pure java***. As you can see, your solution works on some PCs and doesn't on some others (like mine). It works sometimes, but you'll need it to work *always*, and that's not the case. The only thing you can do is to try with JNI. You can build your C/C++ file with a simple `system("cls")` call in it, then compile it to a dll and load it from your java application, to me it seems the only reliable way to do it. Anyway, if you plan to use this function on different OS, you'll need to compile the library on different OS too. – BackSlash Feb 11 '14 at 15:39
  • This looks like a pretty comprehensive answer: http://stackoverflow.com/a/4889027/10077 – Fred Larson Feb 11 '14 at 15:40
  • @BackSlash I understand that would be the ideal solution, but so far I am just concerned about having it working on my computer. – dabadaba Feb 11 '14 at 15:41
  • @dabadaba If the Windows 7 terminal doesn't like `cls` launched by a java application, then you have two solutions: use JNI or print a bunch of empty lines. Maybe you can also use JCurses but I didn't try it so I don't know if it will work. – BackSlash Feb 11 '14 at 15:49

1 Answers1

1

Try removing the qoutes from around cls on the second option, you are still just passing the string. Without the quotes you are using the String[] you defined.

String[] cls = new String[] {"cmd.exe", "/c", "cls"};
Runtime.getRuntime().exec(cls); 
mikemil
  • 1,213
  • 10
  • 21
  • sorry that was a typo, and no, it does not work. – dabadaba Feb 11 '14 at 15:16
  • @dabadaba: you’re right, it doesn’t work, though it doesn’t produce errors. Interestingly, it seems no-one haven’t noticed the problem of *output redirection* over all the years, in which `Runtime.exec` based solution proposals were made. See [here](http://stackoverflow.com/a/33379766/2711488) – Holger Oct 28 '15 at 09:52