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)