kyle k
kyle k

Reputation: 5512

android get stderr of command

How can i get the stderr of a python binary? It is showing the output of the python script but it is not showing the errors that might be in the script, what java method should i be using? or is there a python command line argument that i can use to display the errors?

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); 
}

Upvotes: 0

Views: 230

Answers (1)

Use Process#getErrorStream(), just like you used getOutputStream().

Upvotes: 1

Related Questions