有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java/bin/sh:1:未找到触摸屏

当使用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) 个答案

  1. # 1 楼答案

    你确定在touchaaa之间有一个合适的空格(0x20)吗

    当您尝试使用参数从sh运行一个不存在的命令时,shell中的“not found”消息不包含以下参数:

    % 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代码的情况下从命令行运行命令,看看它是否可以从那里运行