| | |
Calling Commands from Servlets
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
How do I call this same program so that I can run from a servlet?
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.
Java Syntax (Toggle Plain Text)
import com.sun.corba.se.spi.orbutil.fsm.Input; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; public class runSystemCommand { public static void main(String args[]) { Runtime r = Runtime.getRuntime(); Process p = null; String test = "/home/syum/test"; String cmd = "gedit " + test; try { p = r.exec(cmd); InputStreamReader isr = new InputStreamReader(p.getInputStream()); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } p.waitFor(); } catch (Exception e) { System.out.println(e); } }
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)
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.
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.
![]() |
Similar Threads
- Send data on a serial port (C++)
- ERROR: supplied argument is not a valid MySQL result resource (PHP)
- calling a jsp from a dos bat file (JSP)
- Quick Reference for Linux Commands (Getting Started and Choosing a Distro)
- commands for install win98se from boot floppy (Windows 95 / 98 / Me)
- Calling Oracle Stored Procedures with ASP (ASP)
- Error Linking KeyLogger.exe (C++)
- Internet Explorer Bug lets you run DOS commands remotely (Web Browsers)
Other Threads in the JSP Forum
- Previous Thread: Date Convertion
- Next Thread: reading .csv using jsp
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






