2

I'm creating a simple Java wrapper for git executable, that I want to use in my app. A small code example:

public static void main(String[] args) {
        String gitpath = "C:/eclipse/git/bin/git.exe";
        File folder = new File("C:/eclipse/teste/ssadasd");
        try {
            folder.mkdirs();
            Runtime.getRuntime().exec(
                    gitpath + " clone git@192.168.2.15:test.git", null,
                    folder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

The code simply never ends the execution.. seems like it has caught inside exec. If I run the git clone via command line, it work as expected. If I try another repository, from github, e.g., it works too.

Someone have a ide for what is going on here? Thanks in advance

caarlos0
  • 20,020
  • 27
  • 85
  • 160
  • 1
    I know it doesn't fix your issue, but using JGit might be easier than creating a wrapper from scratch: http://eclipse.org/jgit/ – jcern Oct 03 '12 at 18:56

3 Answers3

2

Runtime.getRuntime().exec returns a Process object that you can use to interact with the process and see what's going on. My suspicion is that you just need to do something like this:

Process p = Runtime.getRuntime().exec(
    gitpath + " clone git@192.168.2.15:test.git", null,
    folder);
p.waitFor();

If not, you can also do getErrorStream() or getOutputStream() on the process to see what it's writing out; that might be helpful in debugging.

Joe K
  • 18,204
  • 2
  • 36
  • 58
2

This isn't a direct answer to your question, but you may want to take a look at JGit, which is direct Java implementation of Git operations (no wrapping of commandline git). JGit gets a lot of use and stabilization work as it is the foundation for EGit (Eclipse Git integration).

Konstantin Komissarchik
  • 28,879
  • 6
  • 61
  • 61
  • i'll try it anyway... http://stackoverflow.com/questions/12734760/jgit-how-to-add-all-files-to-staging-area – caarlos0 Oct 04 '12 at 19:52
1

Runtime.exec() can cause hanging under various circumstances - see this article which quotes the Javadoc, which says (in JDK 7):

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

The article gives some example solutions, which consume the output and error streams, although I think the ProcessBuilder class was introduced after the article was written, so may be more satisfactory: the newer Javadoc adds:

Where desired, subprocess I/O can also be redirected using methods of the ProcessBuilder class.

DNA
  • 42,007
  • 12
  • 107
  • 146