23

我已经搜索了很多地方,但还没有找到有用的东西......必须有办法!所以,我需要的是在 Eclipse 中清除控制台的代码(使其空白)。不,不打印 50 个空白行,清除它!

我找到了这个:

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();
    }

但它给了我一个错误:“方法 clear() 未定义控制台类型

4

2 回答 2

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!

于 2013-04-08T15:09:31.603 回答
14

在 Eclipse 工具中,您可以通过右键单击+清除来清除控制台面板,但在 Java 中则不行。

控制台是一个日志工具,为了管理安全,它不能被清除。

于 2012-05-09T06:41:44.550 回答