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
Author

Runtime.exec problems with servlets.

Anuradha Krishna
Greenhorn

Joined: Mar 14, 2004
Posts: 6
Hi,
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,
Anuradha
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
Try this..
http://forum.java.sun.com/thread.jsp?thread=65667&forum=33&message=165938
Serach for "Runtime.exec() Servlets" on Google, that will also be useful.
SAF
Anuradha Krishna
Greenhorn

Joined: Mar 14, 2004
Posts: 6
Sorry,
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,
Anuradha
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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()
fBill
Anuradha Krishna
Greenhorn

Joined: Mar 14, 2004
Posts: 6
Hi,
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,
Anu
import java.io.*;
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) {
out.println(str);
try {
Thread.sleep(100);
} catch (Exception e) {}
}
inR.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Please this URL
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
This might help
Cheers
_Saj
 
 
subject: Runtime.exec problems with servlets.