Solved

Clearing the console

Posted on 2003-10-27
18
401 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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 100 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

728 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