Solved

waitFor() method hangs even though I handle the streams

Posted on 2006-11-20
34
743 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
  • 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
 

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 250 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 250 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 250 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now