【发布时间】: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 这很有趣。很高兴知道未来......