Java无法运行程序CreateProcess error = 2

时间:2017-04-14 10:14:07

标签: java git shell

我想运行一个使用java类" DefaultExecutor"执行shell脚本的命令,但是我收到此错误:

Cannot run program "get_encrypted_password.sh" (in directory "C:\Temp\scripts"): CreateProcess error=2 specified file not found".

该脚本适用于git bash。

有人可以告诉我我哪里做错了吗?

public Entity updateWithEncryptedPassword(Entity entity) throws IOException {
    String password = entity.getPwd();

    String security_key = "00000000000000000000000000000000";

    String path = "C:/Temp/scripts"; 

    CommandLine commandLine = CommandLine.parse("get_encrypted_password.sh");

    commandLine.addArgument(password);

    commandLine.addArgument(security_key);

    String encrypted_password = Utils.runCommandAndGetOutput(commandLine, path);

    entity.setNewPwd(encrypted_password);

    return super.update(entity);
}

public static String runCommandAndGetOutput(CommandLine commandLine, String path) throws IOException {
    DefaultExecutor defaultExecutor = new DefaultExecutor();
    defaultExecutor.setExitValue(0);
    defaultExecutor.setWorkingDirectory(new File(path));

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);

    defaultExecutor.setStreamHandler(streamHandler);

    defaultExecutor.execute(commandLine);

    return outputStream.toString();
}

1 个答案:

答案 0 :(得分:0)

执行“bash”(可能是git bash,)并将“get_encrypted_pa​​ssword.sh”作为参数传递给它,而不是执行“get_encrypted_pa​​ssword.sh”,以便bash执行你的脚本