How to make a simple console output

Hi Community,

I'd like to print some text like System.out.println(text); in the user console but I don't know how.

Markus

17 comments

In what console? There is no such thing as the user console.

0

I just want to inform the user who uses my plugin about a running process.
This process is a running Runtime.exec(cmd) command that runs some tests with maven.

0

Thank you but thats not quite the thing that I want.

I want to make a console output like the maven plugin, but I don't get how this could be done.

something like this:

[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building messaging 1.3.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ messaging ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO]
[INFO] --- maven-dependency-plugin:2.7:copy (copy) @ messaging ---

0

Then you probably need to implement your own toolwindow for displaying the output (or use the existing Messages toolwindow managed by the MessageView service).

In general, if you want to do something exactly like what another plugin does, the best thing you can do is look at the source of the plugin and see how it's implemented.

0

Now I've got my own ToolWindow and I read in this thread http://devnet.jetbrains.com/message/5224984#5224984
that it is possible to add a ConsoleView to the ToolWindow, but how can this be done?

And how can I access the console later?

0

I got a solution, but theres a little problem.

I've done it like this:

 ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ID);
ConsoleView consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).getConsole();
Content content = toolWindow.getContentManager().getFactory().createContent(consoleView.getComponent(), "A Console Foo Bar", true);
toolWindow.getContentManager().addContent(content);


And the output of the process is printed like this:

 InputStreamReader isr = new InputStreamReader(process.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line = null;

while ((line = br.readLine()) != null) {
      consoleView.print(line + "\n", ConsoleViewContentType.NORMAL_OUTPUT);
}


The problem is, that the text in the console appears only when the process has stoped running.
Did I miss something?

0

Calling ConsoleView.attachToProcess() removes the need to do manual input reading. Please do consider looking at the existing code implementing similar functionality.

0

Please do consider looking at the existing code implementing similar functionality.


Thats exactly what I try to do, but I just can't get an output.

0

Doesn't attachToProcess() work for you?

0

Here is what I tried:

String command = "mvn.bat -f " + execPath + " -Dtest=" + psiClass.getName() + " test";
Process process = Runtime.getRuntime().exec(command);

ProcessHandler processHandler = new DefaultJavaProcessHandler(process, null, Charset.defaultCharset());
consoleView.attachToProcess(processHandler);


and the ConsoleView ist attached to the ToolWindow like above.

I used processHandler.notifyTextAvailable() too but the text only appears if the process terminates.

But this approach does not work, I don't get an output in the ConsoleView.

0

You need to call startNotify() on the process handler before calling attachToProcess().

0

Thank you very much, now it works.

There is only one porblem left. The output is printed if the process has finished running but I think this is a problem with my ToolWindow.

0

Hi Markus,

I am trying to implement a very similar feature these days. I have tried your method there is no output on the console. I dont know why... Have you figured out the problem? Thank you so much!

Best,
Luke

0

Thank you so much for your quick reply! The approach really helps me a lot! I think I got the same problem as you that is I was trying to use "Runtime.getRuntime().exec(command, null, envDir)" method to create a new process and print the results on the console and event log. But output are printed after the process finishing running. Did you figure it out as well? Thanks!

0

What I wanted to do is to run a JUnit Test programmatically and to print the test output in a console.

I achieved this by creating a runconfiguration programmatically with the testclasses in it.
The ProgrammRunnerUtil class is able to execute these runcofiguration and prints the output automatically in a console.

Markus

0

Please sign in to leave a comment.