javaapache-commons-exec

How to execute /bin/sh with commons-exec?


This is what I'm doing:

import org.apache.commons.exec.*;
String cmd = "/bin/sh -c \"echo test\"";
new DefaultExecutor().execute(CommandLine.parse(cmd));

This is the output:

/bin/sh: echo test: command not found

What am I doing wrong?


Solution

  • This one works for me:-

        CommandLine command = new CommandLine("/bin/sh");
        command.addArguments(new String[] {
                "-c",
                "echo 'test'"
        },false);
        new DefaultExecutor().execute(command);