Re: Clear the Screen

From: Anthony Borla (
Date: 01/09/04

Date: Fri, 09 Jan 2004 13:28:44 GMT

"Denz" <> wrote in message
> Ive seen this question before- with no real solution.
> Surely it wouldnt have been difficult to provide a clear
> screen, and even basic screen cursor positioning?
> It would make text-mode java alot more useful.

Unfortunately these tasks are inherently operating system-specific, and
cannot be implemented in any standard way across platforms. Some platforms,
in fact, don't even understand the concepts of 'screen', or 'cursor', so
wouldn't even have any use for classes that implemented these abstractions.
Even the 'standard' C and C++ languages which, like Java, aim for platform
independance, do not implement such functionality.

System-specific tasks such as these may be performed via:

* 'Runtime.getRuntime().exec(...)'

* Writing Java Native Interface [JNI] routines which tap into
   the relevant operating system functionality

The first option should really be seen as a 'quick and dirty' approach - it
allows you to perform certain system-specific tasks but with very little
control. A program, to be considered robust, would make no more than sparing
use of this facility. If you find you are relying on it heavily you may need
to seriously question whether Java is the ideal development tool for this
task [a scripting language (Perl, Python) might be more suitable ?], or more
of a development commitment needs be made by using JNI.

There is nothing inherently wrong with JNI except that its use helps defeat
one of Java's most important qualities - platform independance - and it
introduces 'weaknesses' into the application because program execution
occurs outside the control of the JVM, and any failure here could be
catastrophic. On the flipside, Java would be a very limited development
environment without it since it allows for the tapping into system-specific
functionality in a controlled, and efficient way.

So, to provide the facilities you seek, you need to implement suitable JNI
routines. Again, options are available:

* Tap into someone else's work. As another respondant has
   already meantioned, the JCurses package provides a
   reasonable suit of screen handling routines for the *NIX
   and Windows-familiy platforms

* Write your own. This isn't as difficult as you might imagine,
   and might be the best approach if all you require is a
   few simple routines like:

   - Clearing the screen
   - Moving the cursor to specified positions
   - Accepting input [say a single keystroke] without pressing

   A good start would be the JNI tutorial at the Sun site, and,
   of course, perusal of the documentation for the system
   functions implementing this functionality

Anyway, apologies for the longwindedness of the response. Hopefully, though,
some useful insights have been provided.


Anthony Borla