Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

waitFor() method hangs even though I handle the streams

Posted on 2006-11-20
34
Medium Priority
?
796 Views
Last Modified: 2008-01-09
The program that I am working on that runs a dos command. This command renders quite a bit of output that comes slowly over the network.
It hangs on the waitFor command even though I handle the streams. Is it becuase of the amount of data in the streams? Regardless I need to resolve the issue. Thanks,

Here is the command it is running

psexec \\aServer -u domain\username -p password -c -v ibmdsa2.exe -b -c -d "\\aComputer\d$\results\aServer"

Here is the execution code which handles the streams (Error and output)

try{
Process proc = Runtime.getRuntime().exec(aStr);
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
return exitVal;
}
catch(Exception e){System.out.println(e); return -1;}



The stream handler thread looks like this:



public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null){System.out.println(line);}
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
0
Comment
Question by:bbcac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 14
  • 4
34 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17980610
How do you know it *has* exited?
0
 

Author Comment

by:bbcac
ID: 17980625
because the program doesn't take that long... I can see, by going to my open sessions that the that session has been closed...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17980647
If you put a System.output.println after

int exitVal = proc.waitFor();

does it get printed?
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:bbcac
ID: 17980664
no... I put one before and after and it prints the one before, but not the one after
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17980769
Have you eliminated network problems
0
 

Author Comment

by:bbcac
ID: 17980787
if I run the same command at the command line, but cutting and pasting from the program to the dos prompt, it works fine
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17980846
I can only sugggest that you take a thread dump. Do Ctrl-Break in your console when it's hung. It will help to identify what you see if you name the threads first if you're not already doing so
0
 

Author Comment

by:bbcac
ID: 17981262
Here is the dump of the output stream... I don't know what to make of it

"Thread-8" prio=6 tid=0x0ac5e980 nid=0x1784 runnable [0x0d79f000..0x0d79fbe8]
        at java.io.FileInputStream.readBytes(Native Method)
        at java.io.FileInputStream.read(Unknown Source)
        at java.io.BufferedInputStream.read1(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source)
        - locked <0x02b620a0> (a java.io.BufferedInputStream)
        at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        - locked <0x02b66128> (a java.io.InputStreamReader)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        - locked <0x02b66128> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(Unknown Source)
        at distributeddsatool.StreamGobbler.run(StreamGobbler.java:22)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17981608
Name the threads then it can be narrowed down - it's one of the 'gobblers'
0
 

Author Comment

by:bbcac
ID: 17981666
How do I name a thread?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17981687
Thread t = new Thread("t");
0
 

Author Comment

by:bbcac
ID: 17981773
"OUTPUT" prio=6 tid=0x0ad0b838 nid=0x173c runnable [0x0b8ef000..0x0b8efbe8]
        at java.io.FileInputStream.readBytes(Native Method)
        at java.io.FileInputStream.read(Unknown Source)
        at java.io.BufferedInputStream.read1(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source)
        - locked <0x02b620a0> (a java.io.BufferedInputStream)
        at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        - locked <0x02b66128> (a java.io.InputStreamReader)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        - locked <0x02b66128> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(Unknown Source)
        at distributeddsatool.StreamGobbler.run(StreamGobbler.java:25)

"Error" prio=6 tid=0x0ad84d70 nid=0x7c4 runnable [0x0b8af000..0x0b8afc68]
        at java.io.FileInputStream.readBytes(Native Method)
        at java.io.FileInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        - locked <0x02b6c2a0> (a java.io.InputStreamReader)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        - locked <0x02b6c2a0> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(Unknown Source)
        at distributeddsatool.StreamGobbler.run(StreamGobbler.java:25)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17981825
>>The stream handler thread looks like this:

How are you creating these and invoking them?
0
 

Author Comment

by:bbcac
ID: 17981840
I invoke them like this

Process proc = Runtime.getRuntime().exec(aStr);
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off

And I start them like this:

errorGobbler.start();
outputGobbler.start();


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17981863
Can you post the StreamGobbler class you're using?
0
 

Author Comment

by:bbcac
ID: 17982010
Certainly

package distributeddsatool;
import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
    InputStream is;
    String type;
    String name;

    StreamGobbler(InputStream is, String type, String aName)
    {
        this.is = is;
        this.type = type;
        this.setName(aName);
    }

    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
           
            while ( (line = br.readLine()) != null){System.out.println(line); try {Thread.sleep(1000);}catch(Exception e){}}
           
            br.close();
            } catch (IOException ioe)
              {

                ioe.printStackTrace();
              }
           
    }
}
0
 

Author Comment

by:bbcac
ID: 17982043
fyi... the Thread.Sleep(1000); portion was just added in an attempt to fix this whole thing.... there or not it doesn't work.

Perhaps this will help, the commad that I am running trickles input for about 10 minutes when you run aStr on the command line. The process is therefore very long. Might that have something to do with my issues?
0
 
LVL 92

Expert Comment

by:objects
ID: 17983243
are you recieiving all the output as it trickles over the wire

could be a problem with psexec, try running the command locally and see if you get the same problem.
0
 

Author Comment

by:bbcac
ID: 17984667
I recieve a bit of the out put.... but then it hangs, always at the same spot it seems

I output the exact command string so that I can cut and paste it to dos... it runs fine there.... If i run the program with my computer name, it also runs fine... it only seems to not work when it is run over the network.
0
 
LVL 92

Expert Comment

by:objects
ID: 17984731
have had others with similiar problems using psexec with RUntime.exec(), do u have any alternatives to using it?
0
 

Author Comment

by:bbcac
ID: 17984738
I have to run that IBMDSA2.exe remotely on a computer. Further more it requires elevated previledges to run such a program... any ideas?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 17984778
have u tried running it in a command shell?  cmd /c psexec ......
0
 

Author Comment

by:bbcac
ID: 17984913
I will tomorrow morning... Should it all be one string?

ie. String cmd = "cmd /c psexec .... "

or should I use an array

ie. String cmd[] = {"cmd", "/c","psexec...."};

0
 
LVL 92

Expert Comment

by:objects
ID: 17984922
all one string is fine
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17985546
>>Further more it requires elevated previledges to run such a program... any ideas?

Use the runas command
0
 

Author Comment

by:bbcac
ID: 17986891
I can't use runas becuase it needs to run on the remote machine, no on mine
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17987121
If you can run one process on the remote machine then you can run another (runas)
0
 

Author Comment

by:bbcac
ID: 17987133
no luck running the program from the command shell

Still does exactly the same thing... is there anyway that I can suppress the output of the program?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 17987276
Append this to the command

2>&1>nul
0
 

Author Comment

by:bbcac
ID: 17987495
appending that command to the end seems to only supress the output of the IBMDSA2.exe protion of the command but it worked this time... I received the proper exit val and the program did not hang... I will test more and let you know...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17987530
Insert it after every executable whose output you want to suppress
0
 

Author Comment

by:bbcac
ID: 17989251
Is there a way in java to easily destry all the pocess that your java program has started?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17989275
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17996722
:-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question