1

I am running my Java program from terminal and I am trying to count the number of files in a certain directory using a linux command in my code; I have managed to get output for all other commands but this one.

My command is: ls somePath/*.xml | wc -l

When I run my command in my code, it appears that it has nothing to output, yet when I run the same exact command in terminal it works just fine and actually outputs the number of xml files in that directory.

Here is my code:

private String executeTerminalCommand(String command) {
    String s, lastOutput = "";
    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        BufferedReader br = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
        System.out.println("Executing command: " + command);
        while ((s = br.readLine()) != null){//it appears that it never enters this loop since I never see anything outputted 
            System.out.println(s);
            lastOutput = s;
        }
        p.waitFor();
        p.destroy();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lastOutput;//returns empty string ""
}

Updated code w/ output

private String executeTerminalCommand(String command) {
        String s, lastOutput = "";
        try {
            Process p = new ProcessBuilder().command("/bin/bash", "-c", command).inheritIO().start();           
            //Process p = Runtime.getRuntime().exec(command);
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            System.out.println("Executing command: " + command);
            while ((s = br.readLine()) != null){
                System.out.println("OUTPUT: " + s);
                lastOutput = s;
            }
            System.out.println("Done with command------------------------");
            p.waitFor();
            p.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("LAST OUTPUT IS: " + lastOutput);
        return lastOutput;
    }

output:

Executing command: find my/path -empty -type f | wc -l
Done with command------------------------
1
LAST OUTPUT IS:
16
  • maybe try using ProcessBuilder and doing pb.redirectErrorStream(true); Commented Aug 26, 2016 at 0:10
  • Try this command: Process p = new ProcessBuilder().command("bash", "-c", "ls somePath/*.xml | wc -l").inheritIO().start(); Commented Aug 26, 2016 at 0:14
  • Thank you guys, but unfortunately those did not work for me. Commented Aug 26, 2016 at 0:19
  • try to fully path your path. Also see this SO about pipes stackoverflow.com/questions/3776195/… Commented Aug 26, 2016 at 0:22
  • 1
    @mrnoobynoob The command works fine on my machine( it outputs the no. of lines). Can you post your code, including my suggestion? Commented Aug 26, 2016 at 0:29

1 Answer 1

2

To execute a pipeline, you have to invoke a shell, and then run your commands inside that shell.

Process p = new ProcessBuilder().command("bash", "-c", command).start();

bash invokes a shell to execute your command and -c means commands are read from string. So, you don't have to send the command as an array in ProcessBuilder.

But if you want to use Runtime then

String[] cmd = {"bash" , "-c" , command};
Process p = Runtime.getRuntime().exec(cmd);

Note: You can check advantages of ProcessBuilder here and features here over Runtime

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

1 Comment

It's nice to see the answer using both ProcessBuilder and Runtime. Most of the answers suggest to use ProcessBuilder and answer the code in ProcessBuilder. It really helped. Thanks!

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.