2

I want to perform git commands through cmd in java and want to validate the whole output received. Expected:

Cloning into 'gitrepo'...

remote: Counting objects: 92, done

remote: Finding sources: 100% (92/92)

remote: Getting sizes: 100% (76/76)

remote: Compressing objects: 98% (2370/2400)

remote: Total 92 (delta 14), reused 89 (delta 14)

Unpacking objects: 100% (92/92), done.

Checking connectivity... done.

Try No.1 - ProcessBuilder

        List<String> commands = new ArrayList<String>();
        commands.add("cmd.exe");
        commands.add("/k");
        commands.add("cd D:/" + " && D: && git clone --depth 1 <url>");
        ProcessBuilder pb = new ProcessBuilder(commands);
        pb.redirectErrorStream(true);

        Process prs = pb.start();
        prs.waitFor();
        BufferedReader input = new BufferedReader(new InputStreamReader(prs.getInputStream()));

        String line = null;
        while((line = input.readLine())!= null) {
            output = output + line;
            System.out.println(line);
        }
        System.out.println(output);
        // final String fileAsText = input.lines().collect(Collectors.joining());
        //System.out.println(fileAsText);

        input.close();

Result: Hangs in While Loop

Try No. 2 - Scanner in new Thread

        ProcessBuilder proc = new ProcessBuilder(commands);
        proc. redirectErrorStream(true);
        proc.redirectOutput();
        final Process p = proc.start();//Runtime.getRuntime().exec(cur_string);
        //Handle streams
        //in

        new Thread(new Runnable(){
               public void run(){
                    Scanner stdin = new Scanner(p.getInputStream());
                    while(stdin.hasNextLine()){
                                System.out.println(stdin.nextLine());
                                if (stdin.nextLine().isEmpty())
                                    break;
                    }
                    System.out.println("exit");
                    stdin.close();
                    }
            }).start();

      //wait
      p.waitFor();
      p.destroyForcibly();

Result - Hangs in while Loop

Try No. 3 - Runnable Class

        public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        List<String> commands = new ArrayList<String>();
            commands.add("cmd.exe");
            commands.add("/k");
            commands.add("cd D:/" + " && D: && git clone --depth 1 <url>");
        ProcessBuilder pb = new ProcessBuilder(commands);
        pb.redirectErrorStream(true);
            try {

             Process prs = pb.start();
             Thread inThread = new Thread(new In(prs.getInputStream()));
             inThread.start();
             Thread.sleep(2000);


            } catch (IOException e) {
             e.printStackTrace();
            }
        }


        class In implements Runnable {
        private InputStream is;

        public In(InputStream is) {
        this.is = is;
        }

            @Override
            public void run() {
                byte[] b = new byte[1024];
                int size = 0;
                try {
                    while ((size = is.read(b)) != -1) {
                    System.out.println(new String(b));
                }
                is.close();
                } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }

            }
        }

Result-- Never returns

Try No. 4 - org.apache.commons.fileupload.utils.stream

        final Process _p = Runtime.getRuntime().exec("cmd.exe /k cd \"D:\\" && D: && git clone --depth 1 <url>");

        // Handle stdout...
        new Thread() {
            public void run() {
                try {
                Streams.copy(_p.getInputStream(), System.out,true);
                } catch (Exception anExc) {
                anExc.printStackTrace();
                }
            }
        }.start();

        // Handle stderr...
        new Thread() {
            public void run() {
            try {
                Streams.copy(_p.getInputStream(), System.out,true);
            } catch (Exception anExc) {
                anExc.printStackTrace();
            }
            }
        }.start();
        _p.waitFor();

Result -- Hangs forever

Try No. 5 -- Crydust/Git.java

https://gist.github.com/Crydust/fd1b94afc52cd0f7dd4c

Result - Return only first statement "Cloning into git.repo..."

Try No. 6 --apache.commomns.executor (Executor Watch dog)

Result - Hangs

Try No. 7 - ztexec library

        String output = new ProcessExecutor().command("git", "clone","--depth","1","url")
        .readOutput(true).directory(new File("D:/git")).execute()
        .outputUTF8();  
        System.out.println(output);

Result - Return only first statement "Cloning into git.repo..."

Note::: Not using jgit library because shallow clone is required which is not yet supported in jgit.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Anu Chawla
  • 415
  • 1
  • 4
  • 19
  • The problem is that the JVM never captures "incomplete" output lines. That means that the process started must output a *newline* character at the end of a line. Obviously git executable does not do this. – Timothy Truckle Sep 26 '17 at 09:06

0 Answers0