Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 14th, 2005, 06:27 AM
Enrique Enrique is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 1 Enrique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m
Reputation Power: 0
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

Reply With Quote
  #2  
Old August 10th, 2005, 02:27 AM
NPH NPH is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 2 NPH User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 10 sec
Reputation Power: 0
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!");	
		}	       
	}				
}
For more help, www.NeedProgrammingHelp.com

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Problem reading streams in a process


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT