これは私がやっていることです:
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
私は何を間違っていますか?
これは私がやっていることです:
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
私は何を間違っていますか?
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);
これは私のために働く: -
CommandLine command = new CommandLine("/bin/sh");
command.addArguments(new String[] {
"-c",
"echo 'test'"
},false);
new DefaultExecutor().execute(command);
シェル コマンド ラインから問題を再現できます。
# /bin/sh -c '"echo test"'
# /bin/sh: echo test: command not found
コマンドを引用しないようにするべきだと思います。