Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: To clear the screen - The Final Definitive Answer!
|
Posted: Feb 27, 2002 4:10 PM
|
|
In DOS (or Windows), cls is an internal command of the command interpreter, so you cannot call it directly from a program. Normally, in a C/C++ program you can solve this problem by calling the command interpreter and having it call the command, e.g.: system("c:\windows\command.com /c cls"); I tried using exec() on this same string in Java, but it doesn't work, perhaps because Java launches a new hidden process or perhaps for some other reason. Anyway, that leaves two reasonable solutions. The first is to write your own little C/C++ program:
#include <conio.h> void main() { clrscr(); }
and run it with exec() instead. I tried this and it works fine.
The other solution is to make a native method that calls the same clrscr() function. That would consist of writing a java file something like this (exception handling omitted for brevity): public class ScreenClearer
{
private native void ClearScreen();
static
{
System.loadLibrary("ScreenClearer");
}
public static void main(String[] args)
{
ScreenClearer app = new ScreenClearer();
app.ClearScreen();
}
}
Which you would then compile, as usual:
javac ScreenClearer.java And then use javah to create the C/C++ header file:
javah -jni ScreenClearer Now, you write your C/C++ file, including that generated header file, which would look like this:
#include <jni.h> #include "ScreenClearer.h" #include <conio.h> // Has clrscr().
JNIEXPORT void JNICALL Java_ScreenClearer_ClearScreen(JNIEnv *, jobject) { clrscr(); }
And then compile your C/C++ file to a DLL. Here is how you would do that with the (free) Borland 32-bit compiler:
bcc32 -WD -IC:\jdk1.3.1_01\include;C:\jdk1.3.1_01\include\win32 ScreenClearer (assuming the file was a cpp file; if it was c, then you need to specify the whole name, ScreenClearer.c).
Mix the ingredients, chill, and voil?! -- a light, tasty dessert.
|
|