2

これは私がやっていることです:

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

これは出力です:

/bin/sh: echo test: command not found

私は何を間違っていますか?

4

5 に答える 5

8

FAQによると、「」CommandLine.addArgument()の代わりに使用することをお勧めしますCommandLine.parse()

だから試してみてください

CommandLine commandLine = new CommandLine("/bin/sh");
commandLine.addArgument("-c");
commandLine.addArgument("echo test", false); // false is important to prevent commons-exec from acting stupid
executor.execute(commandLine);
于 2011-02-22T15:40:01.230 に答える
3

これは私のために働く: -

    CommandLine command = new CommandLine("/bin/sh");
    command.addArguments(new String[] {
            "-c",
            "echo 'test'"
    },false);
    new DefaultExecutor().execute(command);
于 2011-02-22T15:47:22.683 に答える
0

シェル コマンド ラインから問題を再現できます。

# /bin/sh -c '"echo test"'
# /bin/sh: echo test: command not found

コマンドを引用しないようにするべきだと思います。

于 2011-02-22T15:34:57.360 に答える