2012-05-09 167 views
19

我已經遍尋搜索,但還沒有找到可行的東西... 還有一種方法!所以,我需要的是在Eclipse中清除控制檯的代碼(使其變爲空白)。不,不打印50空白行,清除它!如何在Java中清除控制檯 - Eclipse SDK

我發現這一點:

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

但它給我一個錯誤:「明確的()是未定義的類型控制檯的方法

+0

https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429 –

+1

對不起,但clear()方法沒有爲java.io.Console定義http://docs.oracle.com/javase /7/docs/api/java/io/Console.html – lfergon

回答

9

在Eclipse工具可以清除控制檯面板通過右鍵點擊 + 明確但不是在Java中。

控制檯是一個日誌工具,它不能被清除以用於管理安全。

+0

OP正在尋找一種以編程方式執行此操作的方法。你也可以點擊Eclipse控制檯右上角的小灰色十字來清除它:) – Jerome

+1

@Jerome由於OP告訴Eclipse你給出了最懶的解決方案。 –

8

我可能是我的回答晚了,但這裏是我能夠做到的(和它的工作對我來說):

我創造了我的控制檯基於此教程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; 
    } 

因此,在其他一些按鈕/控制/監聽器什麼的,我有:

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

,每當這段代碼被執行,我的控制檯被清除。希望能幫助到你。

編輯:忘了提,我在創建RCP應用程序時做了這個!