好吧,我們來看看吧: 當我通過static main傳遞參數時,我的程序工作正常。但是當我通過在main中聲明的相同「參數」時,不起作用。如果聽起來很混亂,這裏是代碼。我所要做的就是做這些東西,沒有參數。 這裏的DEBUG:http://i.stack.imgur.com/sbGyo.pngmain(String [] args)不同的String [] args = {.....}?
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();
}
}
請給我們看看你的堆棧跟蹤「不工作」沒有幫助我們 –
它應該工作。像Philipp說的那樣,請張貼堆棧跟蹤。 – Someone
同時打印數組並檢查它們是否相等 –