OK, here we go: When I pass arguments through the static main, my program works fine. but when I pass the same "arguments" declaring inside main, doesnt works. If sounds confused, here's the code. All I want is to do these thing works with no args through main. Here's the DEBUG: https://i.stack.imgur.com/sbGyo.png
import java.io.*;
import java.util.*;
public class DoProcessBuilder extends Thread {
public static void main (String[] args) throws IOException, InterruptedException {
DoProcessBuilder teste = new DoProcessBuilder();
String[] uia = {"ls","-al","|","grep","bash"};
teste.ExecCommand(uia); // here this not works, WHY? if I execute the "java DoProcessBuilder ls -al | grep bash" works fine?
teste.ExecCommand(args); // works fine!
}
public String ExecCommand(String args[]) throws IOException, InterruptedException {
StringBuffer x = new StringBuffer();
if (args.length <= 0) {
System.err.println("Need command to run");
}
Process process = new ProcessBuilder(args).start();
process.waitFor();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = "";
while((line = br.readLine()) != null){
x.append(line+"\n");
}
System.out.println("\nCOMMAND OUT \n"+x.toString());
return x.toString();
}
}