[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.pde] How to integrate ProcessBuilder + Job + MessageConsole

Hi,
I am very new to eclipse plugin development and I have what I believe is a simple question.


I am trying to run an external process using the java ProcessBuilder and then print its output to MessageConsole. Here is what I am currently doing:

ArrayList<String[]> commands = new ArrayList<String[]>();
String[] com1 = {"first", "command"};
String[] com2 = {"second", "command"};
commands.add (com1);
commands.add (com2);

for (String[] command: commands)
{
final ProcessBuilder pb = new ProcessBuilder(command);
try {
pb.redirectErrorStream(true);
Process proc = pb.start();
InputStreamReader converter = new InputStreamReader(proc.getInputStream());
BufferedReader in = new BufferedReader(converter);
String CurLine;
while ((CurLine = in.readLine()) != null)
Print (CurLine);
//in.close();
} catch (final Exception e) {
System.out.println("Failed to start " + Arrays.toString(command) + ": " + e);
}
}
}

private void Print (String s)
{
MessageConsole myConsole = findConsole(CONSOLE_NAME);
MessageConsoleStream out = myConsole.newMessageStream();
out.println(s);
}

private MessageConsole findConsole(String name) {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
IConsole[] existing = conMan.getConsoles();

for (int i = 0; i < existing.length; i++)
if (name.equals(existing[i].getName()))
return (MessageConsole) existing[i];

//no console found, so create a new one
MessageConsole myConsole = new MessageConsole(name, null);
conMan.addConsoles(new IConsole[]{myConsole});
return myConsole;
}



The first problem (which is probably obvious) is that I am not scheduling a job, so the processes cause the workspace to hang till its done. The second problem is that the output doesn't show up in the MessgeConsole not just till one process is done, but until BOTH processes are done. Once both processes are done, this huge chunk of text from both their output streams is pasted in the MessageConsole. It would be preferable that the output is pasted in the MessageConsole as it is recieved from the external process.


I guess my questions are as follows:
1. Is there a separate class that comes with the eclipse api that I can use in place of the java ProcessBuilder?
2. Once I use a Job to schedule the processes will the output get to the MessageConsole incrementally? Or will it still just come out in one huge chunk with the way I have done it?


Help will be much appreciated, and please include cod snippets if you can :)

Thanks!!!