0

I am using ProcessBuilder in order to execute bash commands in Java, so I implemented this simple method that handles the command as a String: it prints the bash command output ( System.out.println(line); ) and it is even returned as a result

public String exec(String command) {
    String line, output = "";
    try {
        ProcessBuilder builder = new ProcessBuilder(command.split(" "));
        Process process = builder.start();

        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

        while ((line = br.readLine()) != null) {
            System.out.println(line);
            output += line + "\n";
        }
    } catch (IOException e) { e.printStackTrace(); }
    return output;
}

With simple commands like ls or ls -l I obtain the output result, but if I use locate -br '^target$' (its purpose is explained here) I do not obtain anything.

Is there something that I am missing from Unix commands or ProcessBuilder? Why does not print the output of the locate command?

(Obviously I tried this command in my terminal and it works there)

Vzzarr
  • 4,600
  • 2
  • 43
  • 80
  • In order to avoid the error of this [user](https://stackoverflow.com/questions/12124935/processbuilder-adds-extra-quotes-to-command-line) I used the `split(" ")` method in order to create an Array that contains the single words of the command as elements; to be clearer, through split I pass to ProcessBuilder the Array {"locate", "-br", "'^target$'"} – Vzzarr Nov 24 '17 at 12:45
  • The single quotes are not part of the regex, and should be removed. When your command line is parsed by the shell, you need quotes to protect the argument from the shell, but you don't have any shell here. – tripleee Nov 27 '17 at 23:08
  • thanks it worked! If you didn't mark this question as duplicate I would sign this answer as correct. They seem to me two different issues, but you are the experienced user in this domain and it's OK – Vzzarr Nov 28 '17 at 17:04
  • Your question didn't contain the problematic code. I suspect this is still a duplicate, but if you want to try to turn it into a reasonably self-contained description of this particular problem, I'll be happy to remove the duplicate flag. – tripleee Nov 28 '17 at 18:08
  • I'm sorry but I don't understand, I'm quite a newbie here in Stackoverflow, as you can see from my score XD I'll delete the question, thanks anyway, you really resolved my problem ;) – Vzzarr Nov 30 '17 at 18:11
  • In some more detail: *I* think the duplicate covers exactly this case. If you still disagree, then you could [edit] this question to show your particular unique problem, and explain how it is different, and the question would be eligible for reopening, and I'll be happy to do that if your edit can convince me that this is a different question which then definitely should exist on this site, ideally also with a good answer which differs from that on the current duplicate. – tripleee Dec 01 '17 at 06:10
  • If you do that, please ping me in a comment; I won't get alerted if you edit. – tripleee Dec 01 '17 at 06:12

0 Answers0