Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » eclipse hang when exec program
eclipse hang when exec program [message #309601] Thu, 02 November 2006 09:41 Go to next message
Eclipse UserFriend
Originally posted by: joneo_ytk81.hotmail.com

hi all,

i'm having problem with eclipse where it hang when execute a program using
the ProcessBuilder. Below is actually how i create the process and print
out the process details in console view. FYI, i didnt implements any
interfaces or classes. i just create a class which extend from Action. Do
i miss out anything? Please advice. thanks.

ProcessBuilder pb = new ProcessBuilder(EXECUTE.toString(),"-g");
pb.directory(path.toFile());
Process p = pb.start();
InputStream is = p.getInputStream();
int c;
StringBuffer sb = new StringBuffer();
while((c=is.read()) != -1)
{
sb.append((char)c);
}
System.out.println(sb.toString());
Re: eclipse hang when exec program [message #309604 is a reply to message #309601] Thu, 02 November 2006 11:05 Go to previous messageGo to next message
Hans Mueller-Dieckert is currently offline Hans Mueller-DieckertFriend
Messages: 32
Registered: July 2009
Member
Hi joneo,

InputStream.read() will block your thread when there is no data
available from the stream and the end of the stream has not been reached.

I guess that this is the reason for your problem.

greetings,
Hans

joneo schrieb:
> hi all,
>
> i'm having problem with eclipse where it hang when execute a program
> using the ProcessBuilder. Below is actually how i create the process and
> print out the process details in console view. FYI, i didnt implements
> any interfaces or classes. i just create a class which extend from
> Action. Do i miss out anything? Please advice. thanks.
>
> ProcessBuilder pb = new ProcessBuilder(EXECUTE.toString(),"-g");
> pb.directory(path.toFile());
> Process p = pb.start();
> InputStream is = p.getInputStream();
> int c;
> StringBuffer sb = new StringBuffer();
> while((c=is.read()) != -1)
> {
> sb.append((char)c);
> }
> System.out.println(sb.toString());
>
>
Re: eclipse hang when exec program [message #309610 is a reply to message #309601] Thu, 02 November 2006 14:02 Go to previous messageGo to next message
Guenther Koegel is currently offline Guenther KoegelFriend
Messages: 56
Registered: July 2009
Member
Dear Joneo,

joneo schrieb:
> hi all,
> ...

when starting a sub-process you have to monitor *THREE* streams in
parallel: Compare with System.in, System.out and System.err, but now you
are on the opposite side. Your process writes to the System.in of the
sub-process and reads/checks the data coming from System.out and
System.err. Most important: all output data may appear any time.

Solution 1: Use a separate Thread for each channel
and one to monitor sub-process termination (+exit status)
==> use blocking IO and thread termination

Solution 2: Use a single thread
==> Poll each channel with InputStream.available() and read only
if data is available. Error stream has highest priority...

<Some code to illustrate>
while (sub-process.isAlive())
{
if (err.available()
err.read()
else if (out.available())
out.read()
else
in.write("some input data for sup-process")

check sub-process.isAlive();
Thread.sleep(...);
}
</code>


Bye,

G&uuml;nther
Re: eclipse hang when exec program [message #309720 is a reply to message #309610] Mon, 06 November 2006 16:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: joneo_ytk81.hotmail.com

sorry i'm new to java and eclipse. may i know what do you mean with
sub-process? is it creating another thread? do you mind to provide more
guidelines? where should i create the thread? where should i put the code
you have shown me? i've tried the below code but is not working. it does
not go in the while loop.can anyone give some guidance? thanks.

Process p = null;

Thread t = new Thread(){
public void run(){
ProcessBuilder pb = new ProcessBuilder(EXECUTE.toString(),"-g");
pb.directory(path.toFile());
p = pb.start();
}
};

t.start();
InputStream is = p.getInputStream();
InputStream es = p.getErrorStream();
OutputStream os = p.getOutputStream();

while(t.isAlive()){
if(es.available())
es.read();
else if (is.available())
is.read();
else
os.write();
}

Guenther Koegel wrote:

> Dear Joneo,

> joneo schrieb:
>> hi all,
>> ...

> when starting a sub-process you have to monitor *THREE* streams in
> parallel: Compare with System.in, System.out and System.err, but now you
> are on the opposite side. Your process writes to the System.in of the
> sub-process and reads/checks the data coming from System.out and
> System.err. Most important: all output data may appear any time.

> Solution 1: Use a separate Thread for each channel
> and one to monitor sub-process termination (+exit status)
> ==> use blocking IO and thread termination

> Solution 2: Use a single thread
> ==> Poll each channel with InputStream.available() and read only
> if data is available. Error stream has highest priority...

> <Some code to illustrate>
> while (sub-process.isAlive())
> {
> if (err.available()
> err.read()
> else if (out.available())
> out.read()
> else
> in.write("some input data for sup-process")

> check sub-process.isAlive();
> Thread.sleep(...);
> }
> </code>


> Bye,

> G&uuml;nther
Re: eclipse hang when exec program [message #309756 is a reply to message #309720] Tue, 07 November 2006 15:07 Go to previous messageGo to next message
Guenther Koegel is currently offline Guenther KoegelFriend
Messages: 56
Registered: July 2009
Member
Dear Joneo,

joneo schrieb:
> sorry i'm new to java and eclipse. may i know what do you mean with
> sub-process? is it creating another thread? do you mind to provide more
> guidelines? where should i create the thread? where should i put the
> code you have shown me? i've tried the below code but is not working. it
> does not go in the while loop.can anyone give some guidance? thanks.

A sub-process is a new program started by your program. It runs in its
own memory space and the only way to communicate with is via input,
output and error streams. As an example type in a Windows command line
"cmd" and you get a sub-process. The parent waits until the termination
of the sub-process (sometimes also named as child process).

> Process p = null;
>
> Thread t = new Thread(){
> public void run(){
> ProcessBuilder pb = new ProcessBuilder(EXECUTE.toString(),"-g");
> pb.directory(path.toFile());
> p = pb.start();
> }
> };
>
> t.start();

The ProcessBuilder is just a convenience class to set-up a new
sub-process and you do not need a separate thread to create the process.
After that you have to handle the Process p you got back from
ProcessBuilder.start().

> InputStream is = p.getInputStream();
> InputStream es = p.getErrorStream();
> OutputStream os = p.getOutputStream();

Yes, these are the streams you need to read from and write to for
communicating with your sub-process. Now you have to check four conditions:

1) Data available on error stream? read and report error message
2) Data available on input stream? read and work with the output
3) Data needed on the output stream? write new data, line by line
4) Sub-process terminated: check exit code,
read all from is and es; done

>
> while(t.isAlive()){
> if(es.available())
> es.read();
> else if (is.available())
> is.read();
> else
> os.write();
> }

// If you insist on some code, here it comes...
int length;
int resultCode;
while(true)
{
if ((length = es.available()) > 0)
{
byte[] b = new byte[length];
es.read(b);
// report error
}
else if (length = is.available()) > 0)
{
// same as above
}
else
{
try
{
resultCode = p.exitValue();
break;
}
catch(IllegalThreadStateException e)
{
Thread.sleep(100);
}
}
}

Loop over all streams. Try to read the exit value which fails if the
process is still alive.

Check the documentation of Process, Runtime.exec and search the Internet
for some tutorials how to start processes and handle the inter-process
communication... You may need to read some additional documentation
about processes in general as the API documentation explains typically
how to do it but not how things work together.

Bye,

G&uuml;nther
Re: eclipse hang when exec program [message #309764 is a reply to message #309756] Tue, 07 November 2006 18:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: joneo_ytk81.hotmail.com

Thanks for the guidance.Actually the reason i'm doing so is to be able to
pipe the process executions to the console view. is it possible?

Guenther Koegel wrote:

> Dear Joneo,

> joneo schrieb:
>> sorry i'm new to java and eclipse. may i know what do you mean with
>> sub-process? is it creating another thread? do you mind to provide more
>> guidelines? where should i create the thread? where should i put the
>> code you have shown me? i've tried the below code but is not working. it
>> does not go in the while loop.can anyone give some guidance? thanks.

> A sub-process is a new program started by your program. It runs in its
> own memory space and the only way to communicate with is via input,
> output and error streams. As an example type in a Windows command line
> "cmd" and you get a sub-process. The parent waits until the termination
> of the sub-process (sometimes also named as child process).

>> Process p = null;
>>
>> Thread t = new Thread(){
>> public void run(){
>> ProcessBuilder pb = new ProcessBuilder(EXECUTE.toString(),"-g");
>> pb.directory(path.toFile());
>> p = pb.start();
>> }
>> };
>>
>> t.start();

> The ProcessBuilder is just a convenience class to set-up a new
> sub-process and you do not need a separate thread to create the process.
> After that you have to handle the Process p you got back from
> ProcessBuilder.start().

>> InputStream is = p.getInputStream();
>> InputStream es = p.getErrorStream();
>> OutputStream os = p.getOutputStream();

> Yes, these are the streams you need to read from and write to for
> communicating with your sub-process. Now you have to check four conditions:

> 1) Data available on error stream? read and report error message
> 2) Data available on input stream? read and work with the output
> 3) Data needed on the output stream? write new data, line by line
> 4) Sub-process terminated: check exit code,
> read all from is and es; done

>>
>> while(t.isAlive()){
>> if(es.available())
>> es.read();
>> else if (is.available())
>> is.read();
>> else
>> os.write();
>> }

> // If you insist on some code, here it comes...
> int length;
> int resultCode;
> while(true)
> {
> if ((length = es.available()) > 0)
> {
> byte[] b = new byte[length];
> es.read(b);
> // report error
> }
> else if (length = is.available()) > 0)
> {
> // same as above
> }
> else
> {
> try
> {
> resultCode = p.exitValue();
> break;
> }
> catch(IllegalThreadStateException e)
> {
> Thread.sleep(100);
> }
> }
> }

> Loop over all streams. Try to read the exit value which fails if the
> process is still alive.

> Check the documentation of Process, Runtime.exec and search the Internet
> for some tutorials how to start processes and handle the inter-process
> communication... You may need to read some additional documentation
> about processes in general as the API documentation explains typically
> how to do it but not how things work together.

> Bye,

> G&uuml;nther
Re: eclipse hang when exec program [message #309796 is a reply to message #309764] Thu, 09 November 2006 08:18 Go to previous messageGo to next message
Guenther Koegel is currently offline Guenther KoegelFriend
Messages: 56
Registered: July 2009
Member
Dear Joneo,

Am I right by assuming that you want to read the output (and error
messages) from the sub-process and print it to your console?

Then your part of the implementation is marked with the comment
// report error
and
// same as above

Read data into the byte array, convert to String and forward it where
you want it and you are done! On "How to send it to the console view" I
have no clue...

Bye

G&uuml;nther

joneo schrieb:
> Thanks for the guidance.Actually the reason i'm doing so is to be able
> to pipe the process executions to the console view. is it possible?
>
> Guenther Koegel wrote:
>
>> Dear Joneo,
>
>> joneo schrieb:
>>> sorry i'm new to java and eclipse. may i know what do you mean with
>>> sub-process? is it creating another thread? do you mind to provide
>>> more guidelines? where should i create the thread? where should i put
>>> the code you have shown me? i've tried the below code but is not
>>> working. it does not go in the while loop.can anyone give some
>>> guidance? thanks.
>
>> A sub-process is a new program started by your program. It runs in its
>> own memory space and the only way to communicate with is via input,
>> output and error streams. As an example type in a Windows command line
>> "cmd" and you get a sub-process. The parent waits until the
>> termination of the sub-process (sometimes also named as child process).
>
>>> Process p = null;
>>>
>>> Thread t = new Thread(){
>>> public void run(){
>>> ProcessBuilder pb = new ProcessBuilder(EXECUTE.toString(),"-g");
>>> pb.directory(path.toFile());
>>> p = pb.start();
>>> }
>>> };
>>>
>>> t.start();
>
>> The ProcessBuilder is just a convenience class to set-up a new
>> sub-process and you do not need a separate thread to create the
>> process. After that you have to handle the Process p you got back from
>> ProcessBuilder.start().
>
>>> InputStream is = p.getInputStream();
>>> InputStream es = p.getErrorStream();
>>> OutputStream os = p.getOutputStream();
>
>> Yes, these are the streams you need to read from and write to for
>> communicating with your sub-process. Now you have to check four
>> conditions:
>
>> 1) Data available on error stream? read and report error message
>> 2) Data available on input stream? read and work with the output
>> 3) Data needed on the output stream? write new data, line by line
>> 4) Sub-process terminated: check exit code,
>> read all from is and es; done
>
>>>
>>> while(t.isAlive()){
>>> if(es.available())
>>> es.read();
>>> else if (is.available())
>>> is.read();
>>> else
>>> os.write();
>>> }
>
>> // If you insist on some code, here it comes...
>> int length;
>> int resultCode;
>> while(true)
>> {
>> if ((length = es.available()) > 0)
>> {
>> byte[] b = new byte[length];
>> es.read(b);
>> // report error
>> }
>> else if (length = is.available()) > 0)
>> {
>> // same as above
>> }
>> else
>> {
>> try
>> {
>> resultCode = p.exitValue();
>> break;
>> }
>> catch(IllegalThreadStateException e)
>> {
>> Thread.sleep(100);
>> }
>> }
>> }
>
>> Loop over all streams. Try to read the exit value which fails if the
>> process is still alive.
>
>> Check the documentation of Process, Runtime.exec and search the
>> Internet for some tutorials how to start processes and handle the
>> inter-process communication... You may need to read some additional
>> documentation about processes in general as the API documentation
>> explains typically how to do it but not how things work together.
>
>> Bye,
>
>> G&uuml;nther
>
>
Re: eclipse hang when exec program [message #310003 is a reply to message #309796] Wed, 15 November 2006 03:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: joneo_ytk81.hotmail.com

sorry for the late reply. besides reading the output and error messages
from the sub-process, i also want to be able to accept input from user
whenever needed and print at the console. kind of like interactive ways.
i've looked into the debug console but their approach is through the
launchconfig file. but for me i dont have any launchconfig file being
created.

Guenther Koegel wrote:

> Dear Joneo,

> Am I right by assuming that you want to read the output (and error
> messages) from the sub-process and print it to your console?

> Then your part of the implementation is marked with the comment
> // report error
> and
> // same as above

> Read data into the byte array, convert to String and forward it where
> you want it and you are done! On "How to send it to the console view" I
> have no clue...

> Bye

> G&uuml;nther

> joneo schrieb:
>> Thanks for the guidance.Actually the reason i'm doing so is to be able
>> to pipe the process executions to the console view. is it possible?
>>
>> Guenther Koegel wrote:
>>
>>> Dear Joneo,
>>
>>> joneo schrieb:
>>>> sorry i'm new to java and eclipse. may i know what do you mean with
>>>> sub-process? is it creating another thread? do you mind to provide
>>>> more guidelines? where should i create the thread? where should i put
>>>> the code you have shown me? i've tried the below code but is not
>>>> working. it does not go in the while loop.can anyone give some
>>>> guidance? thanks.
>>
>>> A sub-process is a new program started by your program. It runs in its
>>> own memory space and the only way to communicate with is via input,
>>> output and error streams. As an example type in a Windows command line
>>> "cmd" and you get a sub-process. The parent waits until the
>>> termination of the sub-process (sometimes also named as child process).
>>
>>>> Process p = null;
>>>>
>>>> Thread t = new Thread(){
>>>> public void run(){
>>>> ProcessBuilder pb = new ProcessBuilder(EXECUTE.toString(),"-g");
>>>> pb.directory(path.toFile());
>>>> p = pb.start();
>>>> }
>>>> };
>>>>
>>>> t.start();
>>
>>> The ProcessBuilder is just a convenience class to set-up a new
>>> sub-process and you do not need a separate thread to create the
>>> process. After that you have to handle the Process p you got back from
>>> ProcessBuilder.start().
>>
>>>> InputStream is = p.getInputStream();
>>>> InputStream es = p.getErrorStream();
>>>> OutputStream os = p.getOutputStream();
>>
>>> Yes, these are the streams you need to read from and write to for
>>> communicating with your sub-process. Now you have to check four
>>> conditions:
>>
>>> 1) Data available on error stream? read and report error message
>>> 2) Data available on input stream? read and work with the output
>>> 3) Data needed on the output stream? write new data, line by line
>>> 4) Sub-process terminated: check exit code,
>>> read all from is and es; done
>>
>>>>
>>>> while(t.isAlive()){
>>>> if(es.available())
>>>> es.read();
>>>> else if (is.available())
>>>> is.read();
>>>> else
>>>> os.write();
>>>> }
>>
>>> // If you insist on some code, here it comes...
>>> int length;
>>> int resultCode;
>>> while(true)
>>> {
>>> if ((length = es.available()) > 0)
>>> {
>>> byte[] b = new byte[length];
>>> es.read(b);
>>> // report error
>>> }
>>> else if (length = is.available()) > 0)
>>> {
>>> // same as above
>>> }
>>> else
>>> {
>>> try
>>> {
>>> resultCode = p.exitValue();
>>> break;
>>> }
>>> catch(IllegalThreadStateException e)
>>> {
>>> Thread.sleep(100);
>>> }
>>> }
>>> }
>>
>>> Loop over all streams. Try to read the exit value which fails if the
>>> process is still alive.
>>
>>> Check the documentation of Process, Runtime.exec and search the
>>> Internet for some tutorials how to start processes and handle the
>>> inter-process communication... You may need to read some additional
>>> documentation about processes in general as the API documentation
>>> explains typically how to do it but not how things work together.
>>
>>> Bye,
>>
>>> G&uuml;nther
>>
>>
Re: eclipse hang when exec program [message #310951 is a reply to message #310003] Fri, 22 December 2006 02:03 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: joneo_ytk81.hotmail.com

hi,

i'm still unable to solve the eclipse hang issue. i just found out that
the program that i exec will call another program during the execution. so
i wonder will is be detected by the process inputStream which i create
earlier to run the 1st program? ok let me explain the execution flow.

i execute the program A through the ProcessBuilder. Internally program A
will call another program B. i use the inputStream from the ProcessBuilder
to capture all the execution process and display it in the console. there
is one part before calling the program B i saw a command line call "echo
off". when i debug into the eclipse program, i found it actually stop
printing when reaching that line and causing the eclipse hang there. i
follow the code given where i put an infinite loop ( while(true){ } ) to
capture the inputStream/errorStream/outputStream. somehow is just won't
continue when reaching that part. does this mean that it failed to detect
the inputStream from program B? but when i run it through the external
tool configuration, it is able to go through and run the program smoothly
and print all the execution process at the console view. but the words is
printed in red color in the console. how can i fix this issue. can someone
help me?
Re: eclipse hang when exec program [message #310967 is a reply to message #310951] Fri, 22 December 2006 15:22 Go to previous messageGo to next message
Guenther Koegel is currently offline Guenther KoegelFriend
Messages: 56
Registered: July 2009
Member
Dear Joneo,

joneo schrieb:
> hi,
>
> i'm still unable to solve the eclipse hang issue. i just found out that
> the program that i exec will call another program during the execution.
> so i wonder will is be detected by the process inputStream which i
> create earlier to run the 1st program? ok let me explain the execution
> flow.

Hmmm, two processes make it much more complicated because you cannot
monitor the communication between both... Are there any debug options
available?

>
> i execute the program A through the ProcessBuilder. Internally program A
> will call another program B. i use the inputStream from the
> ProcessBuilder to capture all the execution process and display it in
> the console. there is one part before calling the program B i saw a
> command line call "echo off". when i debug into the eclipse program, i
> found it actually stop printing when reaching that line and causing the
> eclipse hang there. i follow the code given where i put an infinite loop
> ( while(true){ } ) to capture the inputStream/errorStream/outputStream.
> somehow is just won't continue when reaching that part. does this mean
> that it failed to detect the inputStream from program B? but when i run
> it through the external tool configuration, it is able to go through and
> run the program smoothly and print all the execution process at the
> console view. but the words is printed in red color in the console. how
> can i fix this issue. can someone help me?
>

Here you have a very ugly situation. Let me suggest some tries:

1) Try to separate both processes
2) Try to call process A without Eclipse and use a batch script
See whether the prints some errors
3) Read the documentation of process A and B for any debug options
4) Use unbuffered io
5) Provide input to both processes, several lines!
And see whether you get error messages
6) Gracefully kill the processes and check for error messages
in your errorStream

Maybe the processes print some error messages but you will not see them
because they are small and the streams are buffered. Or the processes
need additional information before the continue... e.g. like the DOS
command del that may ask you whether you really want to delete the file.

Hope that helps

G&uuml;nther
Re: eclipse hang when exec program [message #310972 is a reply to message #310951] Fri, 22 December 2006 19:54 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wegener.cboenospam.com

joneo wrote:
> hi,
>
> i'm still unable to solve the eclipse hang issue. i just found out that
> the program that i exec will call another program during the execution.
> so i wonder will is be detected by the process inputStream which i
> create earlier to run the 1st program? ok let me explain the execution
> flow.
>
> i execute the program A through the ProcessBuilder. Internally program A
> will call another program B. i use the inputStream from the
> ProcessBuilder to capture all the execution process and display it in
> the console. there is one part before calling the program B i saw a
> command line call "echo off". when i debug into the eclipse program, i
> found it actually stop printing when reaching that line and causing the
> eclipse hang there. i follow the code given where i put an infinite loop
> ( while(true){ } ) to capture the inputStream/errorStream/outputStream.
> somehow is just won't continue when reaching that part. does this mean
> that it failed to detect the inputStream from program B? but when i run
> it through the external tool configuration, it is able to go through and
> run the program smoothly and print all the execution process at the
> console view. but the words is printed in red color in the console. how
> can i fix this issue. can someone help me?
>

It sounds like you are stuck on a blocking call. What is in your
infinite while loop? You indicate that you are capturing the input,
error, and output streams. I don't understand how you can capture all
of these. The Process InputStream (retrieved by calling
process.getOutputStream()) is used to write data to the processes input
stream, you can't capture this in a while loop.

You can capture data from the streams returned by the
process.getInputStream() and the process.getErrorStream() methods by
calling one of the read() methods. Note that if there isn't any data
available on the InputStream when you issue the read() call, the call
will block until data is available. You can call the available() method
to see if anything is available to be read in.
Re: eclipse hang when exec program [message #310981 is a reply to message #310972] Sat, 23 December 2006 04:18 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: joneo_ytk81.hotmail.com

Guys thanks for the replies. i really appreciate it. i've been waiting for
somebody to come rescue me for long time ago. below are the source code on
what i've done so far to capture the process execution output. temporary i
focus on capturing the output from the process first. well i've tried run
the same execution through the external tools configuration. it run
smoothly without having this kind of problem. i debug the code and found
that it is using the ProcessConsole function which required the
configuration setup. so i'm not able to use it's method. can anybody
explain to me how the debug console process flow is? coz i still not clear
on the magic behind through debugging the code. please....

This is what i've just discover lately and i hope this is useful to you
guys. FYI, my program A is written in C++. Program A will call program B
which is written in Perl. One thing i just discovered is that program B
will call another program C (i've no idea what language it is written in
but it is an .exe program which will check on the file checksum). i've no
access to modify the program C code as it come in exe. so what i've done
it to modify the program B to print out the result return by program C.
through the debugging, i found that it actually did execute the program C
but it stopped for the third time of executing it. all the value return by
program C is able to be print out by program B. but i've no idea why it
stop at the third time calling the program C. it is able to run smoothly
when i execute it through command prompt. one thing miss out from the
execution is that the program C will also print out some
information/comments but it is not captured by the inputStream of the
process. is this the one causing the execution to hang? when execute
through the external tool, all the information/comments that return by the
program C is highlighted in red color. what does it means?

Actually previously there are 2 solution suggested by Guenther Koegel

Solution 1: Use a separate Thread for each channel
and one to monitor sub-process termination (+exit status)
==> use blocking IO and thread termination


Solution 2: Use a single thread
==> Poll each channel with InputStream.available() and read only
if data is available. Error stream has highest priority...

the one i'm using is the solution 2. so how is the solution 1 code looks
like?
i not really understand how can i use separate thread for each stream
channel since i only hav one process. so can anybody guide me on this?
sorry i'm still new to eclipse and java programming. please help me.
thanks in advance.

################### Source Code ######################################

try
{
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
MessageConsole myConsole = new MessageConsole("AA", null);
conMan.addConsoles(new IConsole[]{myConsole});
conMan.showConsoleView(myConsole);
MessageConsoleStream ocons = myConsole.newMessageStream();

p = DebugPlugin.exec(cmd, path.toFile());
InputStream is = p.getInputStream();
InputStream es = p.getErrorStream();
is.mark(50);

int rc;
while(true)
{
if (es.available()>0)
{
//os.write(es.available());
//System.out.println("die");
}
else if (is.available()>0)
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s;
while((s = br.readLine()) != null)
{
ocons.println(s);
}
}
else
{
System.out.println("Here");
try
{
rc = p.exitValue();
System.out.println("rC : "+rc);
break;
}
catch(IllegalThreadStateException e)
{
System.out.println("slp");
Thread.sleep(1000);
//is.reset();
}
}
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}

############################################################ #####

David Wegener wrote:

> joneo wrote:
>> hi,
>>
>> i'm still unable to solve the eclipse hang issue. i just found out that
>> the program that i exec will call another program during the execution.
>> so i wonder will is be detected by the process inputStream which i
>> create earlier to run the 1st program? ok let me explain the execution
>> flow.
>>
>> i execute the program A through the ProcessBuilder. Internally program A
>> will call another program B. i use the inputStream from the
>> ProcessBuilder to capture all the execution process and display it in
>> the console. there is one part before calling the program B i saw a
>> command line call "echo off". when i debug into the eclipse program, i
>> found it actually stop printing when reaching that line and causing the
>> eclipse hang there. i follow the code given where i put an infinite loop
>> ( while(true){ } ) to capture the inputStream/errorStream/outputStream.
>> somehow is just won't continue when reaching that part. does this mean
>> that it failed to detect the inputStream from program B? but when i run
>> it through the external tool configuration, it is able to go through and
>> run the program smoothly and print all the execution process at the
>> console view. but the words is printed in red color in the console. how
>> can i fix this issue. can someone help me?
>>

> It sounds like you are stuck on a blocking call. What is in your
> infinite while loop? You indicate that you are capturing the input,
> error, and output streams. I don't understand how you can capture all
> of these. The Process InputStream (retrieved by calling
> process.getOutputStream()) is used to write data to the processes input
> stream, you can't capture this in a while loop.

> You can capture data from the streams returned by the
> process.getInputStream() and the process.getErrorStream() methods by
> calling one of the read() methods. Note that if there isn't any data
> available on the InputStream when you issue the read() call, the call
> will block until data is available. You can call the available() method
> to see if anything is available to be read in.
Re: eclipse hang when exec program [message #310995 is a reply to message #310981] Sat, 23 December 2006 18:15 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wegener.cboenospam.com

See comments in code below

joneo wrote:
> Guys thanks for the replies. i really appreciate it. i've been waiting
> for somebody to come rescue me for long time ago. below are the source
> code on what i've done so far to capture the process execution output.
> temporary i focus on capturing the output from the process first. well
> i've tried run the same execution through the external tools
> configuration. it run smoothly without having this kind of problem. i
> debug the code and found that it is using the ProcessConsole function
> which required the configuration setup. so i'm not able to use it's
> method. can anybody explain to me how the debug console process flow is?
> coz i still not clear on the magic behind through debugging the code.
> please....
>
> This is what i've just discover lately and i hope this is useful to you
> guys. FYI, my program A is written in C++. Program A will call program B
> which is written in Perl. One thing i just discovered is that program B
> will call another program C (i've no idea what language it is written in
> but it is an .exe program which will check on the file checksum). i've
> no access to modify the program C code as it come in exe. so what i've
> done it to modify the program B to print out the result return by
> program C. through the debugging, i found that it actually did execute
> the program C but it stopped for the third time of executing it. all the
> value return by program C is able to be print out by program B. but i've
> no idea why it stop at the third time calling the program C. it is able
> to run smoothly when i execute it through command prompt. one thing miss
> out from the execution is that the program C will also print out some
> information/comments but it is not captured by the inputStream of the
> process. is this the one causing the execution to hang? when execute
> through the external tool, all the information/comments that return by
> the program C is highlighted in red color. what does it means?
> Actually previously there are 2 solution suggested by Guenther Koegel
> Solution 1: Use a separate Thread for each channel
> and one to monitor sub-process termination (+exit status)
> ==> use blocking IO and thread termination
>
>
> Solution 2: Use a single thread
> ==> Poll each channel with InputStream.available() and read only
> if data is available. Error stream has highest priority...
>
> the one i'm using is the solution 2. so how is the solution 1 code looks
> like?
> i not really understand how can i use separate thread for each stream
> channel since i only hav one process. so can anybody guide me on this?
> sorry i'm still new to eclipse and java programming. please help me.
> thanks in advance.

Multiple Thread approach:
declare is and es final (final InputStream is = p.getInputStream()).
Replace the infinite loop below with the following:

Thread isThread = new Thread() {
public void run() {
BufferedReader isReader = new BufferedReader(new
InputStreamReader(is));
String isString;
// You don't care that the readLine will block
// because it is running in a different thread.
while ((isString = isReader.readLine()) != null) {
// This may need to be executed on the Display
// Thread. If so, you need to wrap the
// ocons.println call in a Display.asyncExec() call.
ocons.println(isString);
}
};
isThread.start();

Thread esThread = new Thread() {
public void run() {
byte[] buffer = new byte[100];
while (es.read(buffer) != -1) {
}
};
esThread.start();

There may be some exception handling missing from this. I haven't
actually tried the code, so there could be some other syntax errors as well.

>
> ################### Source Code ######################################
>
> try
> {
> ConsolePlugin plugin = ConsolePlugin.getDefault();
> IConsoleManager conMan = plugin.getConsoleManager();
> MessageConsole myConsole = new MessageConsole("AA", null);
> conMan.addConsoles(new IConsole[]{myConsole});
> conMan.showConsoleView(myConsole);
> MessageConsoleStream ocons = myConsole.newMessageStream();
>
> p = DebugPlugin.exec(cmd, path.toFile());
> InputStream is = p.getInputStream();
> InputStream es = p.getErrorStream();
> is.mark(50);
>
> int rc;
> while(true)
> {
> if (es.available()>0)
> {

Most likely, you problem is here. You never read anything from the
ErrorStream of the process. The processes error stream is a buffer.
When it fills up, the process (any any subprocess) will block if it
attempts to write to the error stream. Once the error stream has data
in it, you will never read from the input stream because you have that
processing in the else of es.available().

From your description, it sounds like program C is writing its data to
the error stream. That is why it shows up in red when run as an
external application. Within this if block, you need to read data from
the error stream. Something like this should work.

byte[] buffer = new byte[es.available()];
es.read(buffer);

> //os.write(es.available());
> //System.out.println("die");
> }
> else if (is.available()>0)
> {

This block of code will also cause you problems. The
BufferedReader.readLine method will block until a complete line (with
terminating line end) is available. Your while loop below will never
exit. The readLine method only returns null if at end of file. This
will only occur if the process exits and closes the input stream you are
reading. Once you enter the loop, you will block on the readLine method
when all of the available data has been read. When the second process
blocks on the error stream mentioned above, there will never be any
additional data writen to the input stream you are blocked on.

Instead of the BufferedReader, I would read the data into a byte array
like shown above.
> BufferedReader br = new BufferedReader(new
> InputStreamReader(is));
> String s;
> while((s = br.readLine()) != null)
> {
> ocons.println(s);
> }
> }
> else
> {
> System.out.println("Here");
> try
> {
> rc = p.exitValue();
> System.out.println("rC : "+rc);
> break;
> }
> catch(IllegalThreadStateException e)
> {
> System.out.println("slp");
> Thread.sleep(1000);
> //is.reset();
> }
> }
> }
> }
> catch (Exception e)
> {
> System.out.println(e.getMessage());
> e.printStackTrace();
> }
>
> ############################################################ #####
>
> David Wegener wrote:
>
>> joneo wrote:
>>> hi,
>>>
>>> i'm still unable to solve the eclipse hang issue. i just found out
>>> that the program that i exec will call another program during the
>>> execution. so i wonder will is be detected by the process inputStream
>>> which i create earlier to run the 1st program? ok let me explain the
>>> execution flow.
>>>
>>> i execute the program A through the ProcessBuilder. Internally
>>> program A will call another program B. i use the inputStream from the
>>> ProcessBuilder to capture all the execution process and display it in
>>> the console. there is one part before calling the program B i saw a
>>> command line call "echo off". when i debug into the eclipse program,
>>> i found it actually stop printing when reaching that line and causing
>>> the eclipse hang there. i follow the code given where i put an
>>> infinite loop ( while(true){ } ) to capture the
>>> inputStream/errorStream/outputStream. somehow is just won't continue
>>> when reaching that part. does this mean that it failed to detect the
>>> inputStream from program B? but when i run it through the external
>>> tool configuration, it is able to go through and run the program
>>> smoothly and print all the execution process at the console view. but
>>> the words is printed in red color in the console. how can i fix this
>>> issue. can someone help me?
>>>
>
>> It sounds like you are stuck on a blocking call. What is in your
>> infinite while loop? You indicate that you are capturing the input,
>> error, and output streams. I don't understand how you can capture all
>> of these. The Process InputStream (retrieved by calling
>> process.getOutputStream()) is used to write data to the processes
>> input stream, you can't capture this in a while loop.
>
>> You can capture data from the streams returned by the
>> process.getInputStream() and the process.getErrorStream() methods by
>> calling one of the read() methods. Note that if there isn't any data
>> available on the InputStream when you issue the read() call, the call
>> will block until data is available. You can call the available()
>> method to see if anything is available to be read in.
>
>
Re: eclipse hang when exec program + console tips [message #311008 is a reply to message #310995] Mon, 25 December 2006 00:19 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: joneo_ytk81.hotmail.com

hi David Wegener,

thanks for the help. is working now. i guess you are right. i've added in
to capture the error stream. and its running smoothly now. i've include my
source code as well in case others would need it as reference. but i got
one question. how can i print the execution process instantly by not
waiting till the process end then only print out the whole things? is it
required to use different thread for that?

################### Source Code ######################################

try
{
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
MessageConsole myConsole = new MessageConsole("AA", null);
conMan.addConsoles(new IConsole[]{myConsole});
conMan.showConsoleView(myConsole);
MessageConsoleStream ocons = myConsole.newMessageStream();

p = DebugPlugin.exec(cmd, path.toFile());
InputStream is = p.getInputStream();
InputStream es = p.getErrorStream();

int rc;
StringBuffer sb = new StringBuffer();
while(true)
{
if (es.available()>0)
{
byte [] bes = new byte[es.available()];
es.read(bes);
for (int i = 0; i<bes.length; i++)
{
sb.append((char)bes[i]);
}
}
else if (is.available()>0)
{
byte [] bis = new byte[is.available()];
is.read(bis);
for (int i = 0; i<bis.length; i++)
{
sb.append((char)bis[i]);
}
}
else
{
try
{
rc = p.exitValue();
break;
}
catch(IllegalThreadStateException e)
{
Thread.sleep(10);
}
}
}
ocons.println(sb.toString());
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}

############################################################ #####


David Wegener wrote:

> See comments in code below

> joneo wrote:
>> Guys thanks for the replies. i really appreciate it. i've been waiting
>> for somebody to come rescue me for long time ago. below are the source
>> code on what i've done so far to capture the process execution output.
>> temporary i focus on capturing the output from the process first. well
>> i've tried run the same execution through the external tools
>> configuration. it run smoothly without having this kind of problem. i
>> debug the code and found that it is using the ProcessConsole function
>> which required the configuration setup. so i'm not able to use it's
>> method. can anybody explain to me how the debug console process flow is?
>> coz i still not clear on the magic behind through debugging the code.
>> please....
>>
>> This is what i've just discover lately and i hope this is useful to you
>> guys. FYI, my program A is written in C++. Program A will call program B
>> which is written in Perl. One thing i just discovered is that program B
>> will call another program C (i've no idea what language it is written in
>> but it is an .exe program which will check on the file checksum). i've
>> no access to modify the program C code as it come in exe. so what i've
>> done it to modify the program B to print out the result return by
>> program C. through the debugging, i found that it actually did execute
>> the program C but it stopped for the third time of executing it. all the
>> value return by program C is able to be print out by program B. but i've
>> no idea why it stop at the third time calling the program C. it is able
>> to run smoothly when i execute it through command prompt. one thing miss
>> out from the execution is that the program C will also print out some
>> information/comments but it is not captured by the inputStream of the
>> process. is this the one causing the execution to hang? when execute
>> through the external tool, all the information/comments that return by
>> the program C is highlighted in red color. what does it means?
>> Actually previously there are 2 solution suggested by Guenther Koegel
>> Solution 1: Use a separate Thread for each channel
>> and one to monitor sub-process termination (+exit status)
>> ==> use blocking IO and thread termination
>>
>>
>> Solution 2: Use a single thread
>> ==> Poll each channel with InputStream.available() and read only
>> if data is available. Error stream has highest priority...
>>
>> the one i'm using is the solution 2. so how is the solution 1 code looks
>> like?
>> i not really understand how can i use separate thread for each stream
>> channel since i only hav one process. so can anybody guide me on this?
>> sorry i'm still new to eclipse and java programming. please help me.
>> thanks in advance.

> Multiple Thread approach:
> declare is and es final (final InputStream is = p.getInputStream()).
> Replace the infinite loop below with the following:

> Thread isThread = new Thread() {
> public void run() {
> BufferedReader isReader = new BufferedReader(new
> InputStreamReader(is));
> String isString;
> // You don't care that the readLine will block
> // because it is running in a different thread.
> while ((isString = isReader.readLine()) != null) {
> // This may need to be executed on the Display
> // Thread. If so, you need to wrap the
> // ocons.println call in a Display.asyncExec() call.
> ocons.println(isString);
> }
> };
> isThread.start();

> Thread esThread = new Thread() {
> public void run() {
> byte[] buffer = new byte[100];
> while (es.read(buffer) != -1) {
> }
> };
> esThread.start();

> There may be some exception handling missing from this. I haven't
> actually tried the code, so there could be some other syntax errors as well.

>>
>> ################### Source Code ######################################
>>
>> try
>> {
>> ConsolePlugin plugin = ConsolePlugin.getDefault();
>> IConsoleManager conMan = plugin.getConsoleManager();
>> MessageConsole myConsole = new MessageConsole("AA", null);
>> conMan.addConsoles(new IConsole[]{myConsole});
>> conMan.showConsoleView(myConsole);
>> MessageConsoleStream ocons = myConsole.newMessageStream();
>>
>> p = DebugPlugin.exec(cmd, path.toFile());
>> InputStream is = p.getInputStream();
>> InputStream es = p.getErrorStream();
>> is.mark(50);
>>
>> int rc;
>> while(true)
>> {
>> if (es.available()>0)
>> {

> Most likely, you problem is here. You never read anything from the
> ErrorStream of the process. The processes error stream is a buffer.
> When it fills up, the process (any any subprocess) will block if it
> attempts to write to the error stream. Once the error stream has data
> in it, you will never read from the input stream because you have that
> processing in the else of es.available().

> From your description, it sounds like program C is writing its data to
> the error stream. That is why it shows up in red when run as an
> external application. Within this if block, you need to read data from
> the error stream. Something like this should work.

> byte[] buffer = new byte[es.available()];
> es.read(buffer);

>> //os.write(es.available());
>> //System.out.println("die");
>> }
>> else if (is.available()>0)
>> {

> This block of code will also cause you problems. The
> BufferedReader.readLine method will block until a complete line (with
> terminating line end) is available. Your while loop below will never
> exit. The readLine method only returns null if at end of file. This
> will only occur if the process exits and closes the input stream you are
> reading. Once you enter the loop, you will block on the readLine method
> when all of the available data has been read. When the second process
> blocks on the error stream mentioned above, there will never be any
> additional data writen to the input stream you are blocked on.

> Instead of the BufferedReader, I would read the data into a byte array
> like shown above.
>> BufferedReader br = new BufferedReader(new
>> InputStreamReader(is));
>> String s;
>> while((s = br.readLine()) != null)
>> {
>> ocons.println(s);
>> }
>> }
>> else
>> {
>> System.out.println("Here");
>> try
>> {
>> rc = p.exitValue();
>> System.out.println("rC : "+rc);
>> break;
>> }
>> catch(IllegalThreadStateException e)
>> {
>> System.out.println("slp");
>> Thread.sleep(1000);
>> //is.reset();
>> }
>> }
>> }
>> }
>> catch (Exception e)
>> {
>> System.out.println(e.getMessage());
>> e.printStackTrace();
>> }
>>
>> ############################################################ #####
>>
>> David Wegener wrote:
>>
>>> joneo wrote:
>>>> hi,
>>>>
>>>> i'm still unable to solve the eclipse hang issue. i just found out
>>>> that the program that i exec will call another program during the
>>>> execution. so i wonder will is be detected by the process inputStream
>>>> which i create earlier to run the 1st program? ok let me explain the
>>>> execution flow.
>>>>
>>>> i execute the program A through the ProcessBuilder. Internally
>>>> program A will call another program B. i use the inputStream from the
>>>> ProcessBuilder to capture all the execution process and display it in
>>>> the console. there is one part before calling the program B i saw a
>>>> command line call "echo off". when i debug into the eclipse program,
>>>> i found it actually stop printing when reaching that line and causing
>>>> the eclipse hang there. i follow the code given where i put an
>>>> infinite loop ( while(true){ } ) to capture the
>>>> inputStream/errorStream/outputStream. somehow is just won't continue
>>>> when reaching that part. does this mean that it failed to detect the
>>>> inputStream from program B? but when i run it through the external
>>>> tool configuration, it is able to go through and run the program
>>>> smoothly and print all the execution process at the console view. but
>>>> the words is printed in red color in the console. how can i fix this
>>>> issue. can someone help me?
>>>>
>>
>>> It sounds like you are stuck on a blocking call. What is in your
>>> infinite while loop? You indicate that you are capturing the input,
>>> error, and output streams. I don't understand how you can capture all
>>> of these. The Process InputStream (retrieved by calling
>>> process.getOutputStream()) is used to write data to the processes
>>> input stream, you can't capture this in a while loop.
>>
>>> You can capture data from the streams returned by the
>>> process.getInputStream() and the process.getErrorStream() methods by
>>> calling one of the read() methods. Note that if there isn't any data
>>> available on the InputStream when you issue the read() call, the call
>>> will block until data is available. You can call the available()
>>> method to see if anything is available to be read in.
>>
>>
Re: eclipse hang when exec program + console tips [message #311013 is a reply to message #311008] Mon, 25 December 2006 03:35 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wegener.cboenospam.com

joneo wrote:
> hi David Wegener,
>
> thanks for the help. is working now. i guess you are right. i've added
> in to capture the error stream. and its running smoothly now. i've
> include my source code as well in case others would need it as
> reference. but i got one question. how can i print the execution process
> instantly by not waiting till the process end then only print out the
> whole things? is it required to use different thread for that?
>

Personally, I would use separate threads for each stream. You can use
blocking IO when you have a dedicated thread and don't need to worry
about putting a sleep into your loop.

As to the issue of waiting till the end to print out the data, you could
call ocons.println() after you have read data in. Instead of appending
all of it to the end of a single StringBuffer, create a String from the
byte array read in each time. Something like this:
es.read(bes);
String besBuffer = new String(bes);
ocons.println(besBuffer);

Another change to consider would be to remove the else from the
is.available() check. It won't cause a big difference, but the way the
code is right now, the InputStream will only be processed if the
ErrorStream has no data on it.

> ################### Source Code ######################################
>
> try
> {
> ConsolePlugin plugin = ConsolePlugin.getDefault();
> IConsoleManager conMan = plugin.getConsoleManager();
> MessageConsole myConsole = new MessageConsole("AA", null);
> conMan.addConsoles(new IConsole[]{myConsole});
> conMan.showConsoleView(myConsole);
> MessageConsoleStream ocons = myConsole.newMessageStream();
>
> p = DebugPlugin.exec(cmd, path.toFile());
> InputStream is = p.getInputStream();
> InputStream es = p.getErrorStream();
>
> int rc;
> StringBuffer sb = new StringBuffer();
> while(true)
> {
> if (es.available()>0)
> {
> byte [] bes = new byte[es.available()];
> es.read(bes);
> for (int i = 0; i<bes.length; i++)
> {
> sb.append((char)bes[i]);
> }
> }
> else if (is.available()>0)
> {
> byte [] bis = new byte[is.available()];
> is.read(bis);
> for (int i = 0; i<bis.length; i++)
> {
> sb.append((char)bis[i]);
> }
> }
> else
> {
> try
> {
> rc = p.exitValue();
> break;
> }
> catch(IllegalThreadStateException e)
> {
> Thread.sleep(10);
> }
> }
> }
> ocons.println(sb.toString());
> }
> catch (Exception e)
> {
> System.out.println(e.getMessage());
> e.printStackTrace();
> }
> ############################################################ #####
>
>
> David Wegener wrote:
>
>> See comments in code below
>
>> joneo wrote:
>>> Guys thanks for the replies. i really appreciate it. i've been
>>> waiting for somebody to come rescue me for long time ago. below are
>>> the source code on what i've done so far to capture the process
>>> execution output. temporary i focus on capturing the output from the
>>> process first. well i've tried run the same execution through the
>>> external tools configuration. it run smoothly without having this
>>> kind of problem. i debug the code and found that it is using the
>>> ProcessConsole function which required the configuration setup. so
>>> i'm not able to use it's method. can anybody explain to me how the
>>> debug console process flow is? coz i still not clear on the magic
>>> behind through debugging the code. please....
>>>
>>> This is what i've just discover lately and i hope this is useful to
>>> you guys. FYI, my program A is written in C++. Program A will call
>>> program B which is written in Perl. One thing i just discovered is
>>> that program B will call another program C (i've no idea what
>>> language it is written in but it is an .exe program which will check
>>> on the file checksum). i've no access to modify the program C code as
>>> it come in exe. so what i've done it to modify the program B to print
>>> out the result return by program C. through the debugging, i found
>>> that it actually did execute the program C but it stopped for the
>>> third time of executing it. all the value return by program C is able
>>> to be print out by program B. but i've no idea why it stop at the
>>> third time calling the program C. it is able to run smoothly when i
>>> execute it through command prompt. one thing miss out from the
>>> execution is that the program C will also print out some
>>> information/comments but it is not captured by the inputStream of the
>>> process. is this the one causing the execution to hang? when execute
>>> through the external tool, all the information/comments that return
>>> by the program C is highlighted in red color. what does it means?
>>> Actually previously there are 2 solution suggested by Guenther Koegel
>>> Solution 1: Use a separate Thread for each channel
>>> and one to monitor sub-process termination (+exit status)
>>> ==> use blocking IO and thread termination
>>>
>>>
>>> Solution 2: Use a single thread
>>> ==> Poll each channel with InputStream.available() and read only
>>> if data is available. Error stream has highest priority...
>>>
>>> the one i'm using is the solution 2. so how is the solution 1 code
>>> looks like?
>>> i not really understand how can i use separate thread for each stream
>>> channel since i only hav one process. so can anybody guide me on
>>> this? sorry i'm still new to eclipse and java programming. please
>>> help me. thanks in advance.
>
>> Multiple Thread approach:
>> declare is and es final (final InputStream is = p.getInputStream()).
>> Replace the infinite loop below with the following:
>
>> Thread isThread = new Thread() {
>> public void run() {
>> BufferedReader isReader = new BufferedReader(new
>> InputStreamReader(is));
>> String isString;
>> // You don't care that the readLine will block
>> // because it is running in a different thread.
>> while ((isString = isReader.readLine()) != null) {
>> // This may need to be executed on the Display
>> // Thread. If so, you need to wrap the
>> // ocons.println call in a Display.asyncExec() call.
>> ocons.println(isString);
>> }
>> };
>> isThread.start();
>
>> Thread esThread = new Thread() {
>> public void run() {
>> byte[] buffer = new byte[100];
>> while (es.read(buffer) != -1) {
>> }
>> };
>> esThread.start();
>
>> There may be some exception handling missing from this. I haven't
>> actually tried the code, so there could be some other syntax errors as
>> well.
>
>>>
>>> ################### Source Code ######################################
>>>
>>> try
>>> {
>>> ConsolePlugin plugin = ConsolePlugin.getDefault();
>>> IConsoleManager conMan = plugin.getConsoleManager();
>>> MessageConsole myConsole = new MessageConsole("AA", null);
>>> conMan.addConsoles(new IConsole[]{myConsole});
>>> conMan.showConsoleView(myConsole);
>>> MessageConsoleStream ocons = myConsole.newMessageStream();
>>> p = DebugPlugin.exec(cmd, path.toFile());
>>> InputStream is = p.getInputStream();
>>> InputStream es = p.getErrorStream();
>>> is.mark(50);
>>>
>>> int rc;
>>> while(true)
>>> {
>>> if (es.available()>0)
>>> {
>
>> Most likely, you problem is here. You never read anything from the
>> ErrorStream of the process. The processes error stream is a buffer.
>> When it fills up, the process (any any subprocess) will block if it
>> attempts to write to the error stream. Once the error stream has data
>> in it, you will never read from the input stream because you have that
>> processing in the else of es.available().
>
>> From your description, it sounds like program C is writing its data
>> to the error stream. That is why it shows up in red when run as an
>> external application. Within this if block, you need to read data
>> from the error stream. Something like this should work.
>
>> byte[] buffer = new byte[es.available()];
>> es.read(buffer);
>
>>> //os.write(es.available());
>>> //System.out.println("die");
>>> }
>>> else if (is.available()>0)
>>> {
>
>> This block of code will also cause you problems. The
>> BufferedReader.readLine method will block until a complete line (with
>> terminating line end) is available. Your while loop below will never
>> exit. The readLine method only returns null if at end of file. This
>> will only occur if the process exits and closes the input stream you
>> are reading. Once you enter the loop, you will block on the readLine
>> method when all of the available data has been read. When the
>> second process blocks on the error stream mentioned above, there will
>> never be any additional data writen to the input stream you are
>> blocked on.
>
>> Instead of the BufferedReader, I would read the data into a byte array
>> like shown above.
>>> BufferedReader br = new BufferedReader(new
>>> InputStreamReader(is));
>>> String s;
>>> while((s = br.readLine()) != null)
>>> {
>>> ocons.println(s);
>>> }
>>> }
>>> else
>>> {
>>> System.out.println("Here");
>>> try
>>> {
>>> rc = p.exitValue();
>>> System.out.println("rC : "+rc);
>>> break;
>>> }
>>> catch(IllegalThreadStateException e)
>>> {
>>> System.out.println("slp");
>>> Thread.sleep(1000);
>>> //is.reset();
>>> }
>>> }
>>> }
>>> }
>>> catch (Exception e)
>>> {
>>> System.out.println(e.getMessage());
>>> e.printStackTrace();
>>> }
>>> ############################################################ #####
>>>
>>> David Wegener wrote:
>>>
>>>> joneo wrote:
>>>>> hi,
>>>>>
>>>>> i'm still unable to solve the eclipse hang issue. i just found out
>>>>> that the program that i exec will call another program during the
>>>>> execution. so i wonder will is be detected by the process
>>>>> inputStream which i create earlier to run the 1st program? ok let
>>>>> me explain the execution flow.
>>>>>
>>>>> i execute the program A through the ProcessBuilder. Internally
>>>>> program A will call another program B. i use the inputStream from
>>>>> the ProcessBuilder to capture all the execution process and display
>>>>> it in the console. there is one part before calling the program B i
>>>>> saw a command line call "echo off". when i debug into the eclipse
>>>>> program, i found it actually stop printing when reaching that line
>>>>> and causing the eclipse hang there. i follow the code given where i
>>>>> put an infinite loop ( while(true){ } ) to capture the
>>>>> inputStream/errorStream/outputStream. somehow is just won't
>>>>> continue when reaching that part. does this mean that it failed to
>>>>> detect the inputStream from program B? but when i run it through
>>>>> the external tool configuration, it is able to go through and run
>>>>> the program smoothly and print all the execution process at the
>>>>> console view. but the words is printed in red color in the console.
>>>>> how can i fix this issue. can someone help me?
>>>>>
>>>
>>>> It sounds like you are stuck on a blocking call. What is in your
>>>> infinite while loop? You indicate that you are capturing the input,
>>>> error, and output streams. I don't understand how you can capture
>>>> all of these. The Process InputStream (retrieved by calling
>>>> process.getOutputStream()) is used to write data to the processes
>>>> input stream, you can't capture this in a while loop.
>>>
>>>> You can capture data from the streams returned by the
>>>> process.getInputStream() and the process.getErrorStream() methods by
>>>> calling one of the read() methods. Note that if there isn't any
>>>> data available on the InputStream when you issue the read() call,
>>>> the call will block until data is available. You can call the
>>>> available() method to see if anything is available to be read in.
>>>
>>>
>
>
icon9.gif  Re: eclipse hang when exec program + console tips [message #694916 is a reply to message #311013] Sun, 10 July 2011 08:28 Go to previous message
Hari Krishnan is currently offline Hari KrishnanFriend
Messages: 3
Registered: July 2011
Junior Member
I want help to do the same as this But my program has to get input from user ie interactive process exec in console. My program prints if the the job is just the console writing job. But I want it to take inputs from console and resume execution and process hangs when it has system call in the program that is executed. Urgent Help me Please!
Previous Topic:jobs are not executing on a pluging inside RCP
Next Topic:run a process in eclipse plugin console
Goto Forum:
  


Current Time: Wed Nov 26 22:06:29 GMT 2014

Powered by FUDForum. Page generated in 0.02329 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software