【问题标题】:Java Cannot run program CreateProcess error=2Java 无法运行程序 CreateProcess 错误=2
【发布时间】:2017-04-14 10:14:06
【问题描述】:

我想使用 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();
}

【问题讨论】:

  • Java 不会让 Windows 能够运行 Unix shell 脚本;有一种方法可以在 Windows 中安装 Unix 服务(Unix 或 Ubuntu 服务),但该过程是 Windows 安装/配置的一部分。
  • @ElliottFrisch 这很有趣。很高兴知道未来......

标签: java git shell


【解决方案1】:

不要执行“get_encrypted_pa​​ssword.sh”,它不能在Windows下运行,而是执行“bash”,(可能是git bash,)并将“get_encrypted_pa​​ssword.sh”作为参数传递给它,这样bash就会执行你的脚本.

【讨论】:

  • 奇怪的是,错误 2 是“找不到文件”,我本来预计会出现一些不同的错误。或者错误可能是“找不到文件”,因为 Windows 试图查找并执行一些“get_encrypted_pa​​ssword.sh.exe”或“get_encrypted_pa​​ssword.sh.bat”
  • 感谢您的回复,我试试看。 :)
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
相关资源
最近更新 更多