Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 808
  • Last Modified:

waitFor() method hangs even though I handle the streams

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
bbcac
Asked:
bbcac
  • 16
  • 14
  • 4
3 Solutions
 
CEHJCommented:
How do you know it *has* exited?
0
 
bbcacAuthor Commented:
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
 
CEHJCommented:
If you put a System.output.println after

int exitVal = proc.waitFor();

does it get printed?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
bbcacAuthor Commented:
no... I put one before and after and it prints the one before, but not the one after
0
 
CEHJCommented:
Have you eliminated network problems
0
 
bbcacAuthor Commented:
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
 
CEHJCommented:
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
 
bbcacAuthor Commented:
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
 
CEHJCommented:
Name the threads then it can be narrowed down - it's one of the 'gobblers'
0
 
bbcacAuthor Commented:
How do I name a thread?
0
 
CEHJCommented:
Thread t = new Thread("t");
0
 
bbcacAuthor Commented:
"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
 
CEHJCommented:
>>The stream handler thread looks like this:

How are you creating these and invoking them?
0
 
bbcacAuthor Commented:
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
 
CEHJCommented:
Can you post the StreamGobbler class you're using?
0
 
bbcacAuthor Commented:
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
 
bbcacAuthor Commented:
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
 
objectsCommented:
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
 
bbcacAuthor Commented:
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
 
objectsCommented:
have had others with similiar problems using psexec with RUntime.exec(), do u have any alternatives to using it?
0
 
bbcacAuthor Commented:
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
 
objectsCommented:
have u tried running it in a command shell?  cmd /c psexec ......
0
 
bbcacAuthor Commented:
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
 
objectsCommented:
all one string is fine
0
 
CEHJCommented:
>>Further more it requires elevated previledges to run such a program... any ideas?

Use the runas command
0
 
bbcacAuthor Commented:
I can't use runas becuase it needs to run on the remote machine, no on mine
0
 
CEHJCommented:
If you can run one process on the remote machine then you can run another (runas)
0
 
bbcacAuthor Commented:
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
 
CEHJCommented:
Append this to the command

2>&1>nul
0
 
bbcacAuthor Commented:
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
 
CEHJCommented:
Insert it after every executable whose output you want to suppress
0
 
bbcacAuthor Commented:
Is there a way in java to easily destry all the pocess that your java program has started?
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 16
  • 14
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now