/ bin / sh: 1: касание не найдено - PullRequest
0 голосов
/ 14 июня 2019

В чем может быть проблема с кодом ниже, когда передано с 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, "");
}

1 Ответ

0 голосов
/ 14 июня 2019

Вы уверены, что между touch и aaa?

имеется правильный пробел (0x20). Когда вы пытаетесь запустить несуществующую команду из sh с параметром, 'notОбнаруженное сообщение от оболочки не содержит параметр:

% 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 и посмотрите, работает ли она оттуда.

...