|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Problem reading streams in a process
Hello to everybody!!
I use exec method in class Runtime to run a program in my code. This method returns an object Process and with this i can read the inputstream and errorstream after the pocess execution. This runs ok. I have another thread that check that the program doesn't spend more time than a specific timeout. When a timeout occurs a destroy method runs to finish program executing. My problem is that i can't read inputstream and errorstream when a destoy method was run. I don't know if i use a good way to do that or i can use a diference solution or i can kill de process ina diference way that let me read the streams (perhaps not kill the process, rather than interrupt this) Any ideas? Thanks a lot!! Kike |
|
#2
|
|||
|
|||
|
I would create a thread whose sole job is to read the output of your process. See code below.
Code:
//run the process
Process p = Runtime.getRuntime().exec(new String[]{"c:/program.exe"});
//start two threads that read from his error and output
new ProcessOutputReader(p.getErrorStream());
new ProcessOutputReader(p.getInputStream());
Code:
//this class represents a thread reads from an inputstream
class ProcessOutputReader extends Thread
{
//The input stream to read from
private InputStream inp;
//constructor
public ProcessOutputReader(InputStream inp)
{
//store the stream
this.inp = inp;
//start the thread
start();
}
public void run()
{
try
{
//create a byte array
byte[] stuff = new byte[40];
//read
while(true)
{
//read some bytes
int read = inp.read(stuff);
//print them out
System.out.print (new String(stuff, 0, read));
}
}
catch(Exception e)
{
System.out.println("Error!");
}
}
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Problem reading streams in a process |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|