aspose file tools*
The moose likes Servlets and the fly likes Runtime.exec problems with servlets. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Runtime.exec problems with servlets." Watch "Runtime.exec problems with servlets." New topic

Runtime.exec problems with servlets.

Anuradha Krishna

Joined: Mar 14, 2004
Posts: 6
I am trying to execute a command using the Runtime.exec() method from within a servlet.
I'm running jdk1.4.2 on Redhat AS 2.1. I have given the complete path for the command to be executed. However, nothing seems to happen. i.e., the command simply does not get executed at all. And a waitFor() seems to wait infinitely.
I have tried giving the following types of commands to be executed:
1. Perl script
2. Shell script (with the shell)
3. Other normal Linux commands like ls etc.
The command still does nto appear to work with whatever I try. Any pointers on this is highly appreciated.
Thanks and regards,
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
Try this..
Serach for "Runtime.exec() Servlets" on Google, that will also be useful.
Anuradha Krishna

Joined: Mar 14, 2004
Posts: 6
I tried this also. Does not seem to help. The Unix commands was an example. A shell script / executable also does not work when done this way.
Any help please ?
Thanks and regards,
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12681
Since you have not yet shown the actual code, we can only guess.
If the process you exec produces any output to standard out or standard err, you MUST provide for consuming the output or the process may well hang forever - exact behavior can't be predicted.
See the java.lang.Process getOutputStream and getErrorStream methods and the discussion in the javadocs for Process. You must set up Threads to consume these streams before entering process.waitFor()

Java Resources at
Anuradha Krishna

Joined: Mar 14, 2004
Posts: 6
I'm attaching a sample Unix specific code. Hope it is okay.
This sample works fine on Solaris, HPUX but not on Redhat Linux AS 2.1.
I'm using jdk 1.4.2 and tomcat version 4.0.4.
However, the problem goes away when I use tomcat version 5.0. Any thoughts ? The problem is I cannot replace tomcat 4.0.4 with tomcat 5.0. Is there anyway I can work around the problem in tomcat 4.0.4 itself ?
Thanks and regards,
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
PrintWriter out = response.getWriter();
Runtime r = Runtime.getRuntime();
out.println("Executing command");
Process p = r.exec("ls /tmp");
try {
String str = null;
InputStream inS = p.getInputStream();
InputStreamReader inSr = new InputStreamReader(inS);
BufferedReader inR = new BufferedReader(inSr);
while ((str = inR.readLine()) != null) {
try {
} catch (Exception e) {}
} catch (IOException ioe) {
Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Please this URL
This might help
I agree. Here's the link:
subject: Runtime.exec problems with servlets.
Similar Threads
executing an sql through java
shell script problems
Shell script
Trapping of different shells like DOS,Unix
Run Oracle Script from within JSP