使用命令行从Java调用Git

8
尝试从Java中调用一个在正常命令行中正确执行的git命令时,我得到了一个奇怪的结果:没有任何输出。
例如,如果我尝试运行以下命令:
public class GitTest {
    public static void main(String args[]) throws IOException{
        String command = "git clone http://git-wip-us.apache.org/repos/asf/accumulo.git";
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line; 
        String text = command + "\n";
        System.out.println(text);
        while ((line = input.readLine()) != null) {
            text += line;
            System.out.println("Line: " + line);
        }
    }
}

我没有任何输出(除了命令,在之前打印)。似乎git仍在下载某些内容,但没有告诉我。也许它会在完成后输出所有内容(所以正常的git调用会输出多少已经准备好,每次更改这一行 - 也许因为这个BufferedReader无法读取完成的行,因此无法输出它)。有没有办法解决这个问题?
2个回答

2

谢谢,StreamGobbler完成了它。 - David Georg Reichelt

0

如果您绝对需要运行命令行git,您可以考虑使用JavaGit(它可能有点过时)。但目前推荐的从Java内部管理Git存储库的方法是使用JGit - Git存储库的纯Java实现。Eclipse EGit插件是基于JGit构建的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接