1

Possible Duplicate:
How to run Unix shell script from java code?

I'm creating a web application with spring mvc, which will be multi-user app. Each user will create own configurations etc etc.

When all configuration is done they should start building and running their project from the web app(executing a shell script from java), today I stumbled upon this post while googling

How to run Unix shell script from Java code?

What is your opinion on this, is there a better way to do this other than Runtime.getRuntime() ...

Community
  • 1
  • 1
London
  • 14,986
  • 35
  • 106
  • 147
  • 2
    possible duplicate of [How to run Unix shell script from java code?](http://stackoverflow.com/questions/525212/how-to-run-unix-shell-script-from-java-code) Consider using the most popular answer, which is an excellent one in your case. And, were you to be in a typically cross-platform need, i would suggest you go the ant way. – Riduidel Dec 29 '10 at 10:21

2 Answers2

0

A little bit better way is to use ProcessBuilder class that provides more convenient API.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

1.

is there a better way to do this other than Runtime.getRuntime()

What do you mean with "better"?

2.

Runtime.getRuntime().exec(myCommand);

This works very well, but keep in mind that the .bashrc (or similar environment setup) will not be executed in this case. Moreover, you don't capture the output.

3. Just as a auxiliary note. Here is my interface to the shell:

import java.io.*;

public class Shell
{
    Process proc;
    BufferedReader in;
    BufferedReader err;
    PrintWriter out;

    public Shell() throws IOException
    {
       proc = Runtime.getRuntime().exec("/bin/bash");
       in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
       err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
       out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);

       exec("source ~/.bashrc");
    }


    public void exec(String cmd)
    {
        out.println(cmd);

        try {
            while (in.ready())
                System.out.println(in.readLine());

            while (err.ready())
                System.err.println(err.readLine());
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public void close()
    {
        try {
            out.println("exit");
            proc.waitFor();

            while (in.ready())
                System.out.println(in.readLine());

            while (err.ready())
                System.err.println(err.readLine());

            in.close();
            out.close();
            proc.destroy();
        } 
        catch (IOException e) {
            e.printStackTrace();
        } 
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void execute(String command) throws IOException
    {
        System.out.println("Executing: " + command);
        Shell shell = new Shell();
        shell.exec(command);
        shell.close();
    }

    public static void main(String[] args) throws IOException
    {   
        Shell.execute("ls -l ~");
    }
}
dagnelies
  • 5,203
  • 5
  • 38
  • 56