使用 Java Process Builder 启动 GitLog [英] Start GitLog with Java Process Builder

查看:72
本文介绍了使用 Java Process Builder 启动 GitLog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过 Java 中的 Processbuilder 启动 GitLog 命令.

I try to start a GitLog command via Processbuilder in Java.

GitLog 命令:

git --git-dir=C:/Users/User/Code/code1/git/.git 日志--pretty=format:"%H \"%an\" %ad \"%s\"" --numstat --date=short

git --git-dir=C:/Users/User/Code/code1/git/.git log --pretty=format:"%H \"%an\" %ad \"%s\"" --numstat --date=short

这是我的代码.Path 是 git 目录的路径.我将 gitpath 硬编码到 git 目录以进行测试.

This is my code. The Path is the path to the git dir. I hardcoded the gitpath to the git dir for testing.

public void createGitLog( Path path ) {
            try
            {          
                String gitpath = "--git-dir=C:/Users/User/Code/code1/git/.git";
                String options = "--pretty=format:\"%H \\\"%an\\\" %ad \\\"%s\\\"\" --numstat --date=short";

                ProcessBuilder builder = new ProcessBuilder("git", gitpath, "log", options );
                Process process = builder.start();

                builder.redirectOutput(ProcessBuilder.Redirect.to( path.resolve("gitlog.dat").toFile() ) );

                int exitValue = process.waitFor();

                if ( exitValue != 0 )
                {
                    // throw
                }
            }
            catch (IOException e) {

            } 
}

如果我在 cmd 中尝试这个命令它可以工作,但在 Java 中我总是得到退出代码 128.

If i try this command in the cmd it works, but in Java I get always the exitcode 128.

这个过程有什么问题?

推荐答案

在我的情况下在终端中运行命令的方法:

That what works in my case to run commands in terminal:

/bin/bash"- bash 路径

"/bin/bash" - path to your bash

-c"- 声明下一个参数是命令

"-c" - states that next param is command

命令"- 您想从终端执行的完整命令(例如 git log --pretty=format:"%H \"%an\" %ad \"%s\"" --numstat --date=short)

"command" - full command you want to execute from terminal (like git log --pretty=format:"%H \"%an\" %ad \"%s\"" --numstat --date=short)

String command = "git " + gitpath + " log " + options;
ProcessBuilder builder = new ProcessBuilder("/bin/bash" , "-c" , command);

如果你想从特定目录启动进程,你也可以在 ProcessBuilder directory() 上使用;

you can also use on ProcessBuilder directory() if you want to start process from specific dir;

 .directory(new File("C:/Users/User/Code/code1/git/"))

这篇关于使用 Java Process Builder 启动 GitLog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆