android获取命令的stderr

时间:2013-11-27 03:57:14

标签: java android python

如何获得python二进制文件的stderr?它显示了python脚本的输出,但它没有显示脚本中可能存在的错误,我应该使用什么java方法?或者是否有可用于显示错误的python命令行参数?

private String exec(String command)
{ 
    try
    {
        String s = getApplicationInfo().dataDir;
        File file2 = new File(s+"/py"); 
        file2.setExecutable(true);
        File externalStorage = Environment.getExternalStorageDirectory();
        String strUri = externalStorage.getAbsolutePath();
        PrintWriter out = new PrintWriter(strUri+"/temp.py");
        out.write(command);
        out.close();
        saveRawToFile();

        Process process = Runtime.getRuntime().exec(s+"/py "+strUri+"/temp.py");
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
        int read; 
        char[] buffer = new char[4096]; 
        StringBuffer output = new StringBuffer(); 
        while ((read = reader.read(buffer)) > 0)
        { 
            output.append(buffer, 0, read); 
        } 
        reader.close(); process.waitFor(); 
        return output.toString(); 
    }
    catch (IOException e)
    { throw new RuntimeException(e); }
    catch (InterruptedException e)
    { throw new RuntimeException(e); } 
}


private void output(final String str)
{ 
    Runnable proc = new Runnable() { 
        public void run()
        { 
            outputView.setText(str);
            outputView.setTextIsSelectable(true);
        } 
    }; 
    handler.post(proc); 
}

1 个答案:

答案 0 :(得分:1)

使用Process#getErrorStream(),就像您使用getOutputStream()一样。