Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is runtime.getruntime().exec("domain specific equivalent of cls") for windows 7

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.

like image 817
Radan Avatar asked Oct 21 '22 16:10

Radan


2 Answers

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.

like image 187
paxdiablo Avatar answered Oct 28 '22 21:10

paxdiablo


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");
}
//------------------------------------------
like image 34
Java42 Avatar answered Oct 28 '22 19:10

Java42