waitFor after exec never returns
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]
Re: waitFor after exec never returns
Thanks for the reply. >Lynx isn't a shell script, you can invoke it directly in the exec() call. The reason I don't invoke lynx directly is because I am redirecting its output to a file, which of course requires a shell. I think that was obscured by some bad line breaks in my original message. Let's see if this is any clearer: /bin/sh -c lynx -dump -force_html 94.text-html > 94.text-plain Ellen -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: waitFor after exec never returns
At 03:31 PM 5/18/2001 -0500, you wrote: >Yes, I noticed the redirection. Have you actually LOOKED at 94.text-plain >when you invoke this command string manually? It generates the right output when I can lynx directly but not when I pass it through a shell. I hadn't realized that lynx would behave differently depending how it was invoked, as long as environment variables were set properly. Thanks for pointing that out. > lynx -dump -force_html 94.text-html > 94.text-plain > >does exactly what you want. Yes, but I can't give that as an argument to Runtime.exec, because it includes redirection. Do you know how to get lynx to do what I want (convert a html file into text)? Ellen Spertus -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
Re: waitFor after exec never returns
At 09:57 PM 5/18/2001 -0500, Joi Ellis wrote: >Lynx is writing the rendered text to stdout, which you're reading and >throwing away. Why not read it and write it to the file yourself? I thought doing it with redirection would be simpler, but apparently not. I rewrote it so my program grabs the lynx output directly, and all works. I still get a hang on p.waitFor, so I replaced it with p.destroy (after I've read the strings). I'd still like to know why waitFor isn't working, but I'm happy my program is running. Thanks for your assistance. Ellen -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]