0

我正在尝试使用apktool来自 Java 程序的。我正在使用它来创建 Web 服务。但是,此命令不会在 Java 程序的 shell 上运行。

String cmd = "apktool d /home/ridhima/Test.apk" ;
try {
    Process p = Runtime.getRuntime().exec(cmd);
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = "";
    while((line = reader.readLine()) != null)
     {
       System.out.print(line + "\n");
     }
    p.waitFor(); 
}
catch (IOException | InterruptedException e1) {
        e1.printStackTrace();
}

该命令直接从 shell 运行得很好。

4

2 回答 2

0

您也许应该等待该过程完成

String cmd = "apktool d /home/ridhima/Test.apk" ;
try {
    Process p = Runtime.getRuntime().exec(cmd);
    // You maybe should wait for the process to complete
    p.waitFor(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = "";
    while((line = reader.readLine()) != null)
     {
       System.out.print(line + "\n");
     }

}
catch (IOException | InterruptedException e1) {
        e1.printStackTrace();
}

或者您可以将ProcessBuilder用于相同的任务

public class Main {
    public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException {
        // Create ProcessBuilder instance for UNIX command ls -l
        java.lang.ProcessBuilder processBuilder = new java.lang.ProcessBuilder("ls", "-l");
        // Create an environment (shell variables)
        java.util.Map env = processBuilder.environment();
        env.clear();
        env.put("COLUMNS", "3");
        processBuilder.directory(new java.io.File("/Users"));
        java.lang.Process p = processBuilder.start();
    }
}
于 2015-03-25T07:49:05.307 回答
0

谢谢,但它现在工作正常。由于 apktool 是一个包装脚本,因此可能无法通过 java 程序识别。直接提取 apktool.jar 即可。

   try {
        ProcessBuilder pb = new ProcessBuilder("/home/ridhima/java/jdk1.8.0/bin/java", "-jar", "apktool.jar","d","test.apk");
        Process p = pb.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while((line = reader.readLine()) != null) {
            System.out.print(line + "\n");
        }
        p.waitFor(); 
    }catch (IOException | InterruptedException e1) {
        e1.printStackTrace();
    }
于 2015-04-21T10:45:53.970 回答