0

I am trying to execute git clone in Java using Java's Runtime.getRuntime().exec() in Linux and the interpreter is /bin/bash. However, I get "no such file or directory" in error stream. I searched the stackoverflow and found no answer can solve my problem. Here is my program in Test.java:

import java.io.*;
public class Test {
    public static void main(String[] args) throws IOException, InterruptedException {
        String version = "10.1.1";
        String repo_url = "https://github.com/postcss/postcss-url";
        String directory = "./tmp";
        String cmd = "\"/usr/bin/git clone --branch " + version + " " + repo_url + " --depth=1 " + directory + "\"";
        // String cmd = "git -h";
        String interpreter = "/bin/bash";
        cmd = " -c "+ cmd;
        System.out.println(interpreter + cmd);
        Process process = Runtime.getRuntime().exec(new String[]{ interpreter, cmd });
        print(process.getInputStream());
        print(process.getErrorStream());
        process.waitFor();
        int exitStatus = process.exitValue();
        System.out.println("exit status: " + exitStatus);
        File[] files = new File(directory).listFiles();
        System.out.println("number of files in the directory: " + files.length);
    }

    public static void print(InputStream input) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader bf = new BufferedReader(new InputStreamReader(input));
                String line = null;
                try {
                    while ((line = bf.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    System.out.println("IOException");
                }
            }
        }).start();
    }
}

./tmp is surely an empty directory. I use javac Test.java to compile the code and then run java Test. Besides, I tried sudo java Test and got the same result. I get output like this:

/bin/bash -c "/usr/bin/git clone --branch 10.1.1 https://github.com/postcss/postcss-url --depth=1 ./tmp"
exit status: 127
/bin/bash:  -c "/usr/bin/git clone --branch 10.1.1 https://github.com/postcss/postcss-url --depth=1 ./tmp": No such file or directory
Exception in thread "main" java.lang.NullPointerException
        at Test.main(Test.java:18)

When I use "git -h" or "ls", it works just fine. But, this command /bin/bash -c "/usr/bin/git clone --branch 10.1.1 https://github.com/postcss/postcss-url --depth=1 ./tmp" works in shell but failed in Java. How can I solve this problem?

1
  • you mean Process process = Runtime.getRuntime().exec(new String[]{ interpreter,"-c", cmd });? I tried just now, but it failed with the same result above. Commented Dec 23, 2020 at 8:04

1 Answer 1

1

You have to pass -c as a separate parameter, and you shouldn't add literal double quotes to the command:

new String[]{ "bash", "-c", "git clone ..." }

This is because spaces and quotes are shell syntax, and Runtime.exec doesn't invoke one to run the command (which happens to be a shell invocation, but that's unrelated)

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

1 Comment

so many thanks, I just remove the quotes and it finally works!

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.