Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Exec() problems on Windows

Thread view: 
MMilkin@gmail.com - 17 Oct 2006 18:30 GMT
Im trying to automate some setupstuff
However im having problems doing this on Windows It seems that the file
gets entered and the first while loop returns all the errors that could
popup. However the second loop is not returning any output that is
occuring and the  program is stalling.

proc = rt.exec("Blah.bat");
stderr = proc.getErrorStream();
isr = new InputStreamReader(stderr);
br = new BufferedReader(isr);

iostr = proc.getInputStream();
ist = new InputStreamReader(iostr);
bri = new BufferedReader(ist);

while ( (line = br.readLine()) != null)
{
               myString = line;
    System.out.println("Error:" + line + ":Error");
}

line = null;

while ( (line = bri.readLine()) != null)
{
    myString = line;
    System.out.println("Error:" + line + ":Error");
}

    System.out.println("waiting for proc");
    exitVal = proc.waitFor();
               
if(exitVal > 0)
{
    System.out.println("exitVal: " + exitVal);
}
MMilkin@gmail.com - 17 Oct 2006 18:35 GMT
Wow never mind (Im sometimes dense) i think i figured it out I needed
to loop on output string inside the first loop.

-Mike

MMil...@gmail.com wrote:
> Im trying to automate some setupstuff
> However im having problems doing this on Windows It seems that the file
[quoted text clipped - 32 lines]
>     System.out.println("exitVal: " + exitVal);
> }
Gordon Beaton - 17 Oct 2006 19:03 GMT
> Wow never mind (Im sometimes dense) i think i figured it out I
> needed to loop on output string inside the first loop.

What you actually need to do is read from both streams concurrently,
i.e. while the process is still running. You will eventually run into
problems if you attempt to read the streams sequentially as you've
shown here, since the process could block while writing to the stream
you haven't started reading yet.

If you use ProcessBuilder you can combine both streams and don't need
the extra Thread to read them.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Nigel Wade - 18 Oct 2006 10:50 GMT
> Im trying to automate some setupstuff
> However im having problems doing this on Windows It seems that the file
[quoted text clipped - 32 lines]
>       System.out.println("exitVal: " + exitVal);
> }

The problem you are running into is the normal catch22 situation with Process
and reading the stdout/stderr streams. You are reading the error stream until
it closes (i.e. the process completes), then you read the output stream. If the
output stream fills the buffer and blocks the process, the process won't exit
so the first while loop won't terminate. If you reverse the order in which you
read the streams it doesn't solve the problem, it only reverses the failure
mode.

What you need to do is read both streams in parallel, and this normally requires
at least 2 threads. Which thread reads which stream, and what the thread does
with the data, is totally dependent on the expected data and how it needs to be
processed.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.