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.
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.
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");
}
//------------------------------------------
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With