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.

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?

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by ~s.o.s~
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.