通过command = touch aaa
传递时,下面的代码可能是什么问题
它抛出/bin/sh: 1: touch aaa: not found
这是代码
boolean isWindows = System.getProperty("os.name")
.toLowerCase().startsWith("windows");
String [] cmd ={"-c", command};
CommandLine cmdLine = new CommandLine("/bin/sh");
cmdLine.addArguments( cmd,false );
if (isWindows) {
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValue(0);
exec.setWorkingDirectory(new File(System.getProperty("user.home")));
int exitCode = exec.execute(cmdLine);
return new Response(exitCode, "");
} else {
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValue(0);
exec.setWorkingDirectory(new File(System.getenv("HOME")));
int exitCode = exec.execute(cmdLine);
return new Response(exitCode, "");
}
答案 0 :(得分:0)
您确定touch
和aaa
之间是否有适当的间隔(0x20)?
当您尝试从sh
运行带有参数的不存在的命令时,来自shell的“未找到”消息不包含参数:
% sh -c 'touchx aaa'
sh: 1: touchx: not found
% dash -c 'touchx aaa'
dash: 1: touchx: not found
% bash -c 'touchx aaa'
bash: touchx: command not found
% busybox sh -c 'touchx aaa'
sh: touchx: not found
尝试从命令行运行没有Java代码的命令,然后查看它是否可以从那里运行。