该程序不允许输入密码,它在读取通过扫描仪输出的命令时基本挂起。基本上我试图从git存储库中获取一个分支。
ProcessBuilder builder = new ProcessBuilder( "cmd" );
builder.redirectErrorStream(true);
Process process = null;
try {
process = builder.start();
} catch (IOException e) {
e.printStackTrace();
}
//get stdin of cmd
BufferedWriter p_stdin = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
// execute the desired command from list
List<String> cmdList = new ArrayList<String>();
String baseURL= "[email protected]/a/b";
cmdList.add("cd c:\rep\project");
cmdList.add(String.format("git fetch -v --progress %s releaseBranch", baseURL));
cmdList.add("exit");
for (String cmd : cmdList) {
try {
//single execution
p_stdin.write(cmd);
p_stdin.newLine();
p_stdin.flush();
if(cmd.startsWith("git fetch")) {
//process.waitFor();
//p_stdin.wait();
p_stdin.write("XXXXX@xxx");
p_stdin.newLine();
p_stdin.flush();
}
} catch (IOException e) {
System.out.println(e);
}
}
Scanner scanner = new Scanner(process.getInputStream());
while ( scanner.hasNextLine()) {
System.out.println(scanner.nextLine()); //Process hangs at this point
}
process.getInputStream().close();
scanner.close();
但控制台挂在
C:\rep\project>git fetch -v --progress [email protected]/a/b releaseBranch
点
[email protected]看起来像一个SSH URL,这意味着您需要一个私钥:
如果您使用的是HTTPS URL,那么如果account in credential helper,您可以考虑缓存密码(这次是密码,而不是密码)。
关键是:最好让Git自己寻找密码/密码(在ssh-agent或凭证助手中),而不是试图通过程序控制台提供相同的敏感日期。