Sometimes, when I create a process with Runtime.exec, waitFor never returns, even though I'm clearing the error and output streams.  Here is my code, with important lines starred:

    static void shexec(String command) throws InternalErrorException {
        String[] commandArray = {"/bin/sh", "-c", command};
        String[] envArray = {"TERM=vt100"};
        Log.logMessage("About to call exec");
        try {
            Log.logMessage("/bin/sh -c " + command);
****        Process p = Runtime.getRuntime().exec(commandArray, envArray);

            InputStream is = p.getErrorStream();
            byte cbuf[] = new byte[1024];
            int len;
            while ((len = is.read(cbuf, 0, 1024)) != -1) {
                if (len > 0) {
                    String errorMessage = new String(cbuf, 0, len);
                    Log.logMessage("Read " + len + " bytes from ErrorStream:" + errorMessage);
                    throw new InternalErrorException(errorMessage);
                }
            }
            Log.logMessage("Done with ErrorStream");

            is = p.getInputStream();
            while ((len = is.read(cbuf, 0, 1024)) != -1)
                Log.logMessage("Read " + len + " bytes from InputStream");
            Log.logMessage("Done with InputStream");

            p.getOutputStream().close();

****        Log.logMessage("About to call waitFor");
****        p.waitFor();
****        Log.logMessage("Return value is " + p.exitValue());
            Log.logMessage("Back from waitFor");
****    } catch (Exception e) {
            Log.logMessage("Exception occurred: " + e);
            throw new InternalErrorException(e);
        }
    }

My log file reads:
About to call exec
/bin/sh -c lynx -dump -force_html /home/systers/javamlm/archive/94.text-html > /home/systers/javamlm/archive/94.text-plain
Done with ErrorStream
Done with InputStream
About to call waitFor
I am using:
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build Blackdown-1.3.0-FCS)
Java HotSpot(TM) Client VM (build Blackdown-1.3.0-FCS, mixed mode)
ps shows the process as being defunct.

This has been frustrating me for a long time.
---------------------------------------------------------------------- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to