File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.exec problems with servlets.

 
Anuradha Krishna
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please this URL
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
This might help
Cheers
_Saj
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic