2012-06-22 40 views
2

我似乎無法得到runtime.exec在我的android應用程序工作。我用shell實用程序主試了一下,這裏是我使用的代碼:不能得到runtime.exec android上工作

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    filesPrinter = (Button) findViewById(R.id.print_files); 
    filesPrinter.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      try { 
       Process proc = Runtime.getRuntime().exec("ls"); 
       out = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())); 
       in = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
       String line; 
       while((line = in.readLine()) != null) { 
        System.out.println(line); 
       } 
       System.out.println("Done reading"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

我沒有得到一個錯誤,但也沒有得到任何logcat中。

+0

什麼是錯誤?問題在哪裏?在發佈之前你有沒有閱讀過SO FAQ? – ArtemStorozhuk

+0

@Astor這個問題似乎是一個非常有效的問題,即使錯誤的更多細節可以幫助確實。 – assylias

+0

我做了一個編輯,沒有錯誤,但我沒有得到任何輸出logcat – MEURSAULT

回答

2

的問題結束了與Eclipse的logcat的錯誤。使用adb logcat,我可以看到應該輸出的所有內容。出於某種原因,Eclipse上的logcat顯示它已連接,但未從仿真器接收任何應用程序級別的輸出。

+0

想一想,我認爲LogCat不會顯示來自標準錯誤流的數據(這是e.printStackTrace()輸出的地方)。嘗試使用'System.out.println(Arrays.toString(e.getStackTrace()));'或者,更好的方法是任何'Log'方法。 –

0

也許您當前的工作目錄(這是什麼ls掃描沒有任何參數)只是不包含任何文件。嘗試提供一個路徑作爲命令參數。

+0

我假設你的意思是「沒有任何東西,除了>>完成閱讀<<」,糾正我,如果我錯了。 –

0

想想你缺少一個proc.waitFor()....

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    filesPrinter = (Button) findViewById(R.id.print_files); 
    filesPrinter.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      try { 
       Process proc = Runtime.getRuntime().exec("ls"); 
       out = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())); 
       in = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
       String line; 
       while((line = in.readLine()) != null) { 
        System.out.println(line); 
       } 
       System.out.println("Done reading"); 
       // 
       proc.waitFor(); // THIS!!! 
       // 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
}