我尝试执行Windows中安装的git命令 (bitbucket mingw32 git)
命令:
Path path = Paths.get("C:\\Users\\My\\AppData\\Local\\Atlassian\\SourceTree\\git_local\\mingw32\\bin");
try {
Utils.runCommand(path,
"git.exe",
"-C",
"c:/product/my_project",
"clone",
"-b",
"develop","https://my_user_name@bitbucket.org/myproject/my_project.git");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
执行git的代码:
public static void runCommand(Path directory, String... command) throws IOException, InterruptedException {
Objects.requireNonNull(directory, "directory");
if (!Files.exists(directory)) {
throw new RuntimeException("can't run command in non-existing directory '" + directory + "'");
}
ProcessBuilder pb = new ProcessBuilder()
.command(command)
.directory(directory.toFile());
Process p = pb.start();
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
outputGobbler.start();
errorGobbler.start();
int exit = p.waitFor();
errorGobbler.join();
outputGobbler.join();
if (exit != 0) {
throw new AssertionError(String.format("runCommand returned %d", exit));
}
}
private static class StreamGobbler extends Thread {
private final InputStream is;
private final String type;
private StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
@Override
public void run() {
try (BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(type + "> " + line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
得到错误:
ERROR> Cloning into 'my_project'...
ERROR> bash: /dev/tty: No such device or address
ERROR> error: failed to execute prompt script (exit code 1)
ERROR> fatal: could not read Password for 'https://user@bitbucket.org': Invalid argument
Exception in thread "main" java.lang.AssertionError: runCommand returned 128
at Utils.runCommand(Utils.java:71)
at Main.main(tester.java:38)