0

I want to create git repository via my java application. I'm trying to do this this way:

builder = new ProcessBuilder(
                "\"C:\\Program Files\\Git\\bin\\sh.exe\" --login" , "git init", "exit");
builder.redirectErrorStream(true);
builder.directory(new File("d:\\repos\\"+newRepoForm.getName()));
p = builder.start();
p.waitFor();
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = reader.readLine()) != null) {
    System.out.println(line);
}

But I get exception:

java.io.IOException: Cannot run program ""C:\Program Files\Git\bin\sh.exe" --login" (in directory "d:\repos\dgngdn"): CreateProcess error=87, The parameter is incorrect

It's the first time I'm doing this kind of stuff, so my mistake can be trivial.

2
  • I think you need to move the "--login" to the second argument. the first needs to be the executable only Commented Dec 28, 2013 at 0:59
  • you're right, git opens, but next commands don't work: "C:/Program Files/Git/bin/sh.exe": git init: No such file or directory Commented Dec 28, 2013 at 1:05

1 Answer 1

3

You might find it easier to use a library rather than to invoke git from the command line. Check out JGit.

More generally, libgit2 is also a popular library for many languages but I don't see a Java binding listed (though in theory it wouldn't be too hard to create one using JNA).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I'll check it, but i'm curious why this particular thing doesn't work
Similarly, you are telling the shell to execute a command named git init but you really want to run the command git with init as an argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.