clrscr() function?

I would like to be able to clear the console window that my java application is running in, but have been unsuccessful in finding a clean way of doing this. The obvious way is to output a bunch of newlines, but I do not want to do that. I have also tried outputing ANSI characters to clear the screen, but that doesn't work on NT because there is no ANSI driver loaded. Is there another way? I am not concerned about platform independence; this solution need only run on win2k. TIA -Keith

Keith, Welcome to JavaRanch! A nice feature of the forums here is the ability to search past conversations for lots of great information. The search page link is at the top right of this page. I did a quick search for "clear" in the subject line on this forum and ran across three conversations that discuss different strategies for clearing the console display. Hopefully one of this will give you some ideas:

Thanks for the welcome, Dirk. I had already searched the forums and come across the threads you've mentioned, none of which lead to the resolution of my question. There appears to be three main approaches to accomplishing the task of clearing the screen: 1) Output enough newlines that the screen is effectively cleared. This approach has one major problem - the number of rows in the console is not constant, ie, i can change the size of my dos window. 2) Output an ANSI sequence forcing a clear screen. This will not work in Windows 2000 as an ANSI driver is not used in the console. 3) Make a call to Runtime.getRuntime().exec(), passing the system-specific command to clear the screen. This does not work either. Though I am not absolutely sure why, it seems like this would create a new console window and then clear it. So, my queston is still unanswered. Any suggestions anyone may have would be greatly appreciated. And I must add, I *love* your forum There is LOTS of great information here. -Keith [ May 21, 2002: Message edited by: Keith Owen ]

I think that the problem is that we have been talking about this for a year now, and no one has come up with a solution yet. Well, if you are DESPARATE you could probably use some JNI to hook into a MS .dll and get the job done. By all means show us how you do it if you try this .

"JavaRanch, where the deer and the Certified play" - David O'Meara

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted May 21, 2002 15:05:00

0

Originally posted by Keith Owen: I *love* your forum There is LOTS of great information here.

Let's keep this Keith guy around!

Keith Owen
Greenhorn

Joined: May 21, 2002
Posts: 19

posted May 22, 2002 12:18:00

0

I've come across a solution that works suitably for me on Win2k and WinXP:

The only downside to this approach is that calling mode.com resizes the console window to whatever values are specified. The values I have used above are the default width and height of a DOS window in Win2k/XP. If mode.com is called with values for cols and lines that are different than the DOS window your java app is running in, the window's size is adjusted to meet the new values. -Keith [ May 22, 2002: Message edited by: Keith Owen ] [ May 22, 2002: Message edited by: Keith Owen ]

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521

posted May 22, 2002 15:34:00

0

Still this is pretty cool I just tested it on a WinNT and it worked just as you described .

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted May 22, 2002 15:59:00

0

I'm sorry to report that it does nothing on my Windows XP machine.

Keith Owen
Greenhorn

Joined: May 21, 2002
Posts: 19

posted May 22, 2002 17:24:00

0

That is odd, Dirk. I'm curious; are you using XP Home or Pro, and which JDK are you running that bit of code with? I am using XP Pro w/JDK 1.3.1_01 and the function above works as it should. I also tested this @ work on a Win2k SP2 box w/same JDK and observed the same result. -Keith

I would expect it to work. If I call the suggested mode.com ... from a command prompt, it works. If I use if from a Java app, nothing happens. Subsequent calls to notepad do indeed start notepad. I would expect this mode.com ... to work.

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted May 22, 2002 18:20:00

0

Curiously, if I execute mode.com without arguments from a command prompt, it does indeed list various port information as would be expected. If I attempt to execute mode.com with or without settings from a Java app, nothing happens and no exceptions are thrown indicating that the specified mode.com application was not found. This is not what I'd expect. Perhaps the 1.4 JVM suppresses the output of command line programs... I'm not sure... Time for some experimenting (aka - wasting time when I should be doing something productive).

Keith Owen
Greenhorn

Joined: May 21, 2002
Posts: 19

posted May 24, 2002 07:01:00

0

Dirk - Any luck in figuring out what the problem is? I have still been unable to get it to *not* work =/ -Keith

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted May 24, 2002 08:22:00

0

No luck.

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted May 26, 2002 13:06:00

0

I can confirm that the exact same code works on a Windows 2000 machine with Java 1.3.1. I'm still at a loss to explain why nothing happens on my home computer running Windows XP and Java 1.4.

Keith Owen
Greenhorn

Joined: May 21, 2002
Posts: 19

posted May 29, 2002 08:28:00

0

I managed to get this working using JNI. I'll post some instructions and a link to the necessary dll later today. -Keith

Keith Owen
Greenhorn

Joined: May 21, 2002
Posts: 19

posted May 29, 2002 11:04:00

0

Here are full instructions on how I accomplished using the clrscr() C function in Java using JNI. I used the Borland C++ 5.02 compiler to make the DLL because VC++ does not provide a native clrscr() function in any of its include headers, so steps on building the DLL are BC++-specific. However, the compiled DLL, which you can download here can be used as-is w/out recompiling. [list]create NativeMethod.java with the following code:

Where "NativeMethod" is the name of the DLL compiled in later steps.[list]run javah on NativeMethod.java to create NativeMethod.h which should look like the following:

add #define __int64 long to NativeMethod.h above the #include <jni.h>

[list]create NativeMethod.c to look like so:

compile the native dll with this command:

bcc32 -tWDE -eNativeMethod.dll -Ic:\jdk1.3.1\include -Ic:\jdk1.3.1\include\win32 NativeMethod.c note that your JDK path may be different than mine.[list] write a driver program to test your new native call. mine is named Example.java and looks like this:

compile the java: c:\>javac NativeMethod.java Example.java

run the app: c:\>java Example

If you want to skip the steps used to create the DLL, download the DLL I have created @ the URL above, create the two java files I listed and then go through the last two steps. -Keith [ May 29, 2002: Message edited by: Keith Owen ] [ May 29, 2002: Message edited by: Keith Owen ]

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted May 29, 2002 11:53:00

0

Very nice. It works well on my Windows XP machine. Has the great Keith Owen finally put this one to rest?

Keith Owen
Greenhorn

Joined: May 21, 2002
Posts: 19

posted May 29, 2002 12:32:00

0

I hope so -Keith

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted May 29, 2002 12:35:00

0

Oh sure, considering yourself great now are you? Well, I never cared much for false modesty anyhow.

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted May 29, 2002 12:37:00

0

Of course, what are those poor Linux and Mac users to do?

Keith Owen
Greenhorn

Joined: May 21, 2002
Posts: 19

posted May 29, 2002 12:51:00

0

Actually, the linux users are probably a bit less poor than the windows users considering they at least received their OS for free Also in their favor is a much easier road to clearing a vt100 terminal. Outputing the ANSI character sequence for clearing the terminal will do the job just fine. As an alternative, you *could* use the JNI method in linux/unix as well, with no code changes, by recompiling the NativeMethod.c into a shared object library (*.so) using gcc instead of making the win32 DLL. I believe a recompile of the java classes is necessary, also. And to the Mac users ... well, they will just have to stay poor until a Mac-acquainted Java programmer strolls along -Keith