The Artima Developer Community
Sponsored Link

Java Answers Forum
To clear the screen

13 replies on 1 page. Most recent reply: Jul 21, 2002 5:18 PM by Lawrenze Chan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 13 replies on 1 page
S Zawng Khaung

Posts: 1
Nickname: cookie
Registered: Feb, 2002

To clear the screen Posted: Feb 27, 2002 2:11 AM
Reply to this message Reply
Advertisement
Hello anyone,

I'm the beginner of Java. Now, I trying to run my programm under MS-DOS. I don't know which method to use to clear the screen. I need to do that for my assignments. So, I need your help.

Please as soon as possible...

Thanks

S Zawng Khaung


Lakshmi

Posts: 1
Nickname: aqb75
Registered: Feb, 2002

Re: To clear the screen Posted: Feb 27, 2002 5:08 AM
Reply to this message Reply
This code assumes a vt100 terminal:

System.out.print("\033[H\033[J");
System.out.flush();

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: To clear the screen Posted: Feb 27, 2002 10:13 AM
Reply to this message Reply
The dos command for clear screen is cls

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
Reply to this message Reply
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.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: To clear the screen - The Final Definitive Answer! Posted: Feb 27, 2002 4:15 PM
Reply to this message Reply
Well, that's interesting, the preview showed "voila" with the accented 'a' just fine, but the actual post gets a '?' instead. That probably means it gets hosed up going into or coming from the database.

aravind

Posts: 1
Nickname: aravind
Registered: Apr, 2002

Re: To clear the screen Posted: Apr 10, 2002 7:15 PM
Reply to this message Reply
hi laxmi , the answre you proviede i tyhink works with unix machine but . i too got teh same problem as i am using dos machine and so please can you help me how to clear the screen when using dos o/s.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: To clear the screen Posted: Apr 11, 2002 12:01 AM
Reply to this message Reply
It's hopeless.

John Edwards

Posts: 1
Nickname: jayz
Registered: May, 2002

Re: To clear the screen Posted: May 31, 2002 3:15 AM
Reply to this message Reply
To clear the screen in java:

System.out.print((char)27 + "[2J");

works for me.

Regards,

John

(www.jaeworld.com - probably the best hangout on the web!)

Lakmini Gunatilake

Posts: 1
Nickname: lak94
Registered: Jun, 2002

Re: To clear the screen Posted: May 31, 2002 11:33 PM
Reply to this message Reply
I know that every one may say this is stupid, But it works


System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: To clear the screen Posted: Jun 1, 2002 1:01 PM
Reply to this message Reply
Okay, you asked for it: That is stupid!

There are differences between clearing the screen and writing a bunch of empty lines to it:
- It looks bad to see stuff scrolling up.
- The screen is not really cleared if you have a scrollable screen as in Unix, Win2k, NT, XP, etc.
- You don't know how many lines there are on the screen. All the above OSes default to more than 25 and even our hoary friend DOS can be configured with more lines, using the mode command.

The way to really clear the screen is with a simple native method that calls clrscr().

Kristoffer Larsson

Posts: 1
Nickname: armageddon
Registered: Jul, 2002

Re: To clear the screen Posted: Jul 16, 2002 4:34 AM
Reply to this message Reply
If that clears your screen then your machine must be enchanted //kind regards

Lawrenze Chan

Posts: 2
Nickname: w00lvie
Registered: Jul, 2002

Re: To clear the screen Posted: Jul 18, 2002 3:49 PM
Reply to this message Reply
Hi,
I'm actually very new with Java. I'm currently doing an assignment with Java and wonder how to clear my DOS screen. I've followed thru the steps and manage to compile the DLL, but how do i call that function from Java? Can anyone please help to clarify (a code snipet will help a lot). thanks

Lawrenz

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: To clear the screen Posted: Jul 18, 2002 4:11 PM
Reply to this message Reply
What steps have you "followed thru" and what DLL have you managed to compile?
Here is a simple sample of Java code to call a native method:
public class ScreenClearer
{
    private native void ClearScreen();
    
    static 
    {
        System.loadLibrary("ScreenClearer");
    }
    
    public static void main(String[] args) 
    {
        ScreenClearer app = new ScreenClearer();
 
        app.ClearScreen();
    }
}

You would then compile the above code
javac ScreenClearer.java
and create the C++ template files like so:
javah -jni ScreenClearer

Then you would edit the C++ code to your liking and build a DLL (I don't know how you've already done this, if you had not first built the template C++ files). Here is how you would build the DLL with Borland's compiler:
bcc32 -WD -IC:\jdk1.3.1_01\include;C:\jdk1.3.1_01\include\win32 ScreenClearer

Now, just run the program as any other Java program:
java ScreenClearer

Lawrenze Chan

Posts: 2
Nickname: w00lvie
Registered: Jul, 2002

Re: To clear the screen Posted: Jul 21, 2002 5:18 PM
Reply to this message Reply
Matt,
It works now. Thanks.

rgds
Laurenz

Flat View: This topic has 13 replies on 1 page
Topic: getting ioexception when closing stream in applet WHY!! Previous Topic   Next Topic Topic: Need Help with JTable and Doc Editor

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use