Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

Is there a way to run "native" commands from jsp?

I have tomcat 6.0 on linux. Is there a way to run native commands such as "ls > ls.out" or whatever?
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

Yes.  Something like this:

Process p = Runtime.getRuntime().exec("ls > ls.out");
I don't believe that will work.  Runtime.exec() is NOT a command line.  The following article explains why:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4

(It's written in the context of Windows, but it applies to Linux as well.)
ASKER CERTIFIED SOLUTION
Avatar of malfunction84
malfunction84
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark
Mark

ASKER

I went ahead and tried the piping command and, as predicted, it didn't work. I also tried Runtime.getRuntime().exec("/bin/sh -c 'echo Howdy | /usr/bin/mail -s testing me@mydomain.com'   >/dev/null 2>&1"); and that didn't work either; which was frankly surprising. Perhaps the stdout/stderr redirects messed it up. I removed those, but the return status was '2', and no message was sent. I may experiment further. I'll have to look up the '2' status.

However, putting it all in a shell script did work fine.
Right -- if you look at the article malfunction84 gave in a post above, you'll see the explanation.  You can't redirect stderr and stdout -- you have to handle them programmatically in your Java program.  So doing "/bin/sh -c 'xxxxx'" is fine, but >/dev/null is not.