如何在Java中清除控制台 - Eclipse SDK

Van*_*duk 21 java clear

我搜索了很多,但没有找到有用的东西......有办法!所以,我需要的是一个清除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();
    }
Run Code Online (Sandbox Code Playgroud)

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

dec*_*ain 11

我的答案可能会迟到,但这是我设法做的(它对我有用):

我根据本教程http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F创建了我的控制台,并将findConsole方法修改为如下所示:

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;
   }
Run Code Online (Sandbox Code Playgroud)

所以,在其他一些按钮/控件/听众的监听器中,我有:

myConsole = findConsole(ASIO_RECORD_OUTPUT);
myConsoleOut = myConsole.newMessageStream();
Run Code Online (Sandbox Code Playgroud)

无论何时执行该代码,我的控制台都会被清除.希望能帮助到你.

编辑:忘了提,我在创建RCP应用程序时这样做了!


cl-*_*l-r 10

在Eclipse工具中,您可以通过右键单击 + clear而不是Java 来清除控制台面板.

控制台是一个日志工具,无法清除管理安全性.

  • @Jerome 正如 OP 所说的 Eclipse,你给出了最懒惰的解决方案。 (2认同)