Calling Commands from Servlets

Reply

Join Date: Oct 2009
Posts: 1
Reputation: syum is an unknown quantity at this point 
Solved Threads: 0
syum syum is offline Offline
Newbie Poster

Calling Commands from Servlets

 
0
  #1
Oct 19th, 2009
Hi all,
I'm having tough time on how to use linux commands from the servlets. I am able to run the commands from java program but I can't run the same program from servlets? Is there a way to do this?
Here is a sample of my code that runs the command $gedit /home/syum/test.txt from a java code and its working fine. But when used a servlet to run the same code, nothing happens.



  1. import com.sun.corba.se.spi.orbutil.fsm.Input;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.InputStreamReader;
  5.  
  6. public class runSystemCommand {
  7.  
  8. public static void main(String args[]) {
  9. Runtime r = Runtime.getRuntime();
  10. Process p = null;
  11. String test = "/home/syum/test";
  12. String cmd = "gedit " + test;
  13. try {
  14. p = r.exec(cmd);
  15. InputStreamReader isr = new InputStreamReader(p.getInputStream());
  16. BufferedReader br = new BufferedReader(isr);
  17. String line = null;
  18. while ((line = br.readLine()) != null) {
  19. System.out.println(line);
  20. }
  21. p.waitFor();
  22. } catch (Exception e) {
  23. System.out.println(e);
  24. }
  25.  
  26. }

How do I call this same program so that I can run from a servlet?
Last edited by peter_budo; Oct 19th, 2009 at 11:36 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,610
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human
 
1
  #2
Oct 21st, 2009
Read Why Runtime.exec() won't work?

If you are using Java 1.5 or more, look into ProcessBuilder class which has replaced the old "exec" way of spawning processes.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC