23

I've searched far and wide, but haven't found something that works... There HAS to be a way!!! So, what I need is a code that clears the console in Eclipse (makes it blank). And NO, not printing 50 blank lines, CLEARING IT!

I found this:

Import import java.io.Console;

public void ClearConsole() {
            Console console = System.console();        
            if (console == null)
                    System.out.println("Couldn't get Console object !");
            console.clear();
    }

But it gives me an error: "The method clear() is undefined for the type Console"

braX
  • 11,506
  • 5
  • 20
  • 33
Vanya Burduk
  • 727
  • 2
  • 8
  • 12

2 Answers2

14

In Eclipse tool you can clear the console panel by right clicking + clear but not in Java.

Console is a log tool, it cannot be cleared for administration security.

cl-r
  • 1,264
  • 1
  • 12
  • 26
  • The OP is looking for a way to do it programmatically. You can also click on the little grey cross on the upper-right corner of the Eclipse console to clear it :) – Jerome May 09 '12 at 06:52
  • 2
    @Jerome As OP told about Eclipse you give the laziest solution. – cl-r May 09 '12 at 07:31
14

I may be late with my answer, but here is what I managed to do (and it worked for me):

I created my console based on this tutorial http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F , and modified the findConsole method to look like this:

private MessageConsole findConsole(String name) {

      ConsolePlugin plugin = ConsolePlugin.getDefault();
      IConsoleManager conMan = plugin.getConsoleManager();

      IConsole[] existing = conMan.getConsoles();
      //if console exists, clear it 
      for (int i = 0; i < existing.length; i++)
          if (name.equals(existing[i].getName())){
              ((MessageConsole) existing[i]).clearConsole(); //this is the important part
              return myConsole;
          }

      myConsole = new MessageConsole(name, null);
      conMan.addConsoles(new IConsole[]{myConsole});
      return myConsole;
   }

So, in the listener of some other button/control/whatever, I have:

myConsole = findConsole(ASIO_RECORD_OUTPUT);
myConsoleOut = myConsole.newMessageStream();

and whenever that piece of code gets executed, my console is cleared. Hope it helps.

edit: Forgot to mention, I did this when creating an RCP application!

deckard cain
  • 497
  • 10
  • 24
  • You need to show the import statements that go with this question or else its a lot of impossible guesswork. – Andrew S Mar 24 '21 at 23:13