[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Clearing the console

Posted on 2003-10-27
18
Medium Priority
?
407 Views
Last Modified: 2013-12-14
Hi, Im mofiying a friends program that finds its way through a maze, and would like to know how to clear the console in java so when I reprint the maze for every step the old step doesnt appear right above it, thanks!
0
Comment
Question by:suns0ft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9630374
Depends on how you're doing this maze, but this might give you a clue as to a possible approach:

public class TestPercent {

      public static void main(String[] args) throws Exception {
            PercentageDisplayer pd = new PercentageDisplayer(500);
            for(int i = 1;i <= 5;i++) {
                  pd.update(100);
                  Thread.sleep(500);
            }
      }

      static class PercentageDisplayer {
            long extent;
            long received;

            public PercentageDisplayer(long extent) {
                  this.extent = extent;
            }

            public void update(long increment) {
                  for(int i = 0;i < 12;i++) {
                        System.out.print('\010');
                  }
                  received += increment;
                  long percentage = 100 * received / extent;
                  System.out.print("" + percentage + "% received");
            }
      }
}
0
 

Author Comment

by:suns0ft
ID: 9630399
ummm Im not a brilliant programmer, but does that at any point clear the console?
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9630549
for(int i = 0;i < 12;i++) {
                    System.out.print('\010');
}

That is the part of the code CEHJ posted that clears the console.  There is no way to clear the console in java like there is in C or C++.  Instead what programmers usually do is put a System.out.println(); in a for loop to push all the text off the page.  This give the appearance that the console is actually being cleared.  
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9630599
Otherwise, you'd have to get hold of a JNI-based API to control the console. I only know of one for Unix
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9630671
There is no API function to clear the console in windows, but you can use
system("cls");
to clear the windows console.
0
 

Author Comment

by:suns0ft
ID: 9631966
thanks, the program is at uni, so I'll try it out tomorrow morning and hand out the points :)
0
 

Author Comment

by:suns0ft
ID: 9637893
Ok I tried both methods, and while the one CEHJ suggested works, it doesnt look very good on the screen. I tried to use system("cls"); and I got a compiler error, is there anything else I need to do to get the one working?
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9637990
system("cls") cant be run from java.  You have to write it in a small c++ method that looks like:

#include <jni.h>
#include "MyClass.h"
#include <stdio.h>

JNIEXPORT void JNICALL
Java_MyClass_clearScreen(JNIEnv *env, jobject obj)
{
    system("cls");
    return;
}

build that code into a dll file, then call the dll from your java program.  If you want help doing that, I can help you do it.  It's a little bit more work than CEHJ's method though.



0
 

Author Comment

by:suns0ft
ID: 9638829
cool, Im not sure how to do that though, if you want to help me i'll increase the point count as well :D
0
 
LVL 5

Accepted Solution

by:
lwinkenb earned 400 total points
ID: 9638992
Ok, I will show you the steps to do this.

First let's create a Java test class:

class ConsoleProg {
      public native void cls();

    static {
        System.loadLibrary("clearconsole");
    }
   
    public static void main(String[] args) throws Exception{
        System.out.println("Some text here!");
        System.out.println("Clearing the console in 5 seconds...");
        Thread.sleep(5000);
        new ConsoleProg().cls();
        System.out.println("Console is cleared");
    }
}

There are two things to note in this class.  First is the command System.loadLibrary().  This basically loads a dll file into memory (in this case clearconsole.dll).  The second thing is the declaration public native void cls().  This is a function inside the clearconsole.dll file we are going to call.

Now compile that class.

Now we need to make a header for the java class.  To do this, just run javah.  For the above class we run:
javah ConsoleProg

Now you should see a file called ConsoleProg.h

Now we need to create the clearconsole.dll file.  To make a dll file that a java program can call, the function declaration must match what is inside the ConsoleProg.h file.  In our case the function declaration looks like:
JNIEXPORT void JNICALL Java_ConsoleProg_cls(JNIEnv *env, jobject obj)
Notice how the function declaration matches our java class.  It is in the form of:
JNIEXPORT <return cls.  void in our case> JNICALL Java_<Java class name>_<function name>(...)

Ok, now we write the code.

#include <jni.h>
#include "ConsoleProg.h"
#include <stdio.h>

JNIEXPORT void JNICALL
Java_ConsoleProg_cls(JNIEnv *env, jobject obj)
{
    system("cls");
    return;
}

Now we need to compile this into a dll file.  To do that, you need a c/c++ compiler.  You can get a free one from Borland, or the GNU compiler is free as well.  Make sure you compile it as a shared library file (.dll, .so, .o).  

The command to do this using Visual C++ compiler is:
cl -Ic:\java\include -Ic:\java\include\win32 -LD clearconsole.c -Feclearconsole.dll
for the GNU compiler it would look something like:
cc -G -Ic:\java\include -Ic:\java\include\win32  clearconsole.c -o clearconsole.dll

Now just run the java file like normal, and everytime you call cls(), the console will be cleared!
0
 

Author Comment

by:suns0ft
ID: 9640331
That looks very complete! I'll try it tomorrow (before my exam lol). And a complier isnt a problem,  the uni runs codewarrior, has java, c, c++ compilers etc. Also has visual C++ so if I cant work the codewarrior one I can follow your instructions.

Anyone have advice on how many points I should give? As it seems lwinkenb has gone to considerable effort to help me here :) Current points at 50, I was thinking of raising it to 100-150 if it works, seem fair?
0
 

Author Comment

by:suns0ft
ID: 11187351
Thanks admin, I'd forgotten about this question, I WAS waiting for someone to give some sort of recommendation as to how many points I should give out (I thought 50 was a little low, wanted some feedback before accepting, as I said in last post), but noone replied , and for some reason I never got a email when Venabili posted his cleanup.. :(

Reading through the answers I see that 50 points is a little low, are you able to add a few more to it after accepting an answer?
0
 

Author Comment

by:suns0ft
ID: 11187379
Cool, well lwinkenb went to a lot of effort helping me, so I think raising the points to 100 seems like a fair bonus, thanks :)
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 11187859
=)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question