Question

When I execute the command I added below from Terminal (using Ubuntu Linux), command is executed properly. But it fails when I try to execute exactly same command through Java application despite there are no errors or exceptions.

Fails means that this command creates a new folder and extract apk file content inside it.

Command to be executed:

java -jar ~/apktool1.5.2/apktool.jar d /home/talha/Desktop/myapk.apk /home/talha/Desktop/myapk

Source code:

Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
Was it helpful?

Solution

Try this code:

    public static void main(String[] args) {    
    String[] cmd = { "/bin/sh", "-c", "java -jar ~/apktool1.5.2/apktool.jar d /home/talha/Desktop/myapk.apk /home/talha/Desktop/myapk" };
    BufferedReader bri = null, bre = null;
    int exitC = 0;
    try {
        Process p = Runtime.getRuntime().exec(cmd);
        exitC = p.waitFor();
        bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
        bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        String line = "";
        while ((line = bri.readLine()) != null) {
            System.out.println(line);               
        }
        while ((line = bre.readLine()) != null) {
            System.out.println(line);
        }
        bri.close();
        bre.close();
    } catch (Exception e) {
        e.printStackTrace();
    } 
    System.out.println("Exit Code: "+ exitC);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top