How to clear console in Java - Eclipse SDK

十年热恋 提交于 2019-11-29 05:56:34

问题


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"


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/10509476/how-to-clear-console-in-java-eclipse-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!