2012-06-22 41 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(); 
      } 
     } 
    }); 
}