tags:

views:

43

answers:

2

Hi,

I have an execute(String cmd) in a jsp script that calls the exec method from the Runtime class.

It works when I call a local command, like a php script stored on the server. for example: /usr/bin/php /path/to/php/script arg1 arg2

So I guess my execute command is ok, since it is working with that.

Now when I try to call lynx, the text-based web browser, it does not work.

If I call it in a terminal, it works fine: /usr/bin/lynx -dump -accept_all_cookies 'http://www.someurl.net/?arg1=1&arg2=2'

But when I call this from my execute command, nothing happens...

Any idea why?

This is my execute method:

public String execute(String cmd){


        Runtime r = Runtime.getRuntime();
        Process p = null;
        String res = "";

        try {
                    p = r.exec(cmd);
                    InputStreamReader isr = new InputStreamReader(p.getInputStream());
                    BufferedReader br = new BufferedReader(isr);
                    String line = null;
                    //out.println(res);
                    while ((line = br.readLine()) != null) {

                    res += line;

                    }
                    p.waitFor();
                    } catch (Exception e) {
                    res += e;
                    }
                    System.out.println(p.exitValue());

        return res;

    }
+1  A: 

You need to read from the Process' output stream.

Since you're not, the underlying lynx process is likely blocking while writing output, waiting for someone to empty the output stream's buffer. Even if you're going to ignore the output, you need to read it anyway for the process to execute as you'd expect.

As the javadocs of Process say, "Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock."

See http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html for some examples of how to handle this.

Edit: in case you are wondering, chances are that when you invoked the PHP script it didn't produce a great deal of output, and so was able to terminate before filling the output buffer and blocking. The lynx command is presumably producing more output and hence hitting this issue.

Andrzej Doyle
hmm ok, so do you think it will work if I just remove the buffer reading and just call the exec method? Because I actually do not need to see the result...I will have a look at the link you posted anyway, thanks for your help!
Piero
I think the buffering may be causing both ends to block, depending on the internal implementations on either side. This is probably what the Javadocs mean by "promptly" read. I'd certainly try getting rid of the buffering, perhaps even replace the whole thing with `(while in.read() != -1) {}`?
Andrzej Doyle
Piero
Could the lynx process perhaps be prompting you for something (perhaps confirmation)? If it wants you to type y/n then it will appear to hang from your point of view. It may be worth logging the contents of the input stream somewhere, so you can see what Lynx is up to, and/or using something like `strace` to see the state of the Lynx process when it hangs.
Andrzej Doyle
A: 

I solved it.... by calling lynx into a php script, php script that I called from the Jsp script...

It's a shitty solution but at least it works... I still do not really understand why the exec command from Java works that way...

Thanks for your help anyway Andrzej (Czech I guess from the name..? ^_^), somehow you put me on the way!

Piero