java运行时exec进程无法正常工作

时间:2014-03-17 09:50:25

标签: java unix jar runtime runtime.exec

当我执行下面从终端(使用Ubuntu Linux)添加的命令时,命令正确执行。但是当我尝试通过 Java应用程序执行完全相同的命令时它失败了,尽管没有错误或异常。

失败表示此命令会创建一个新文件夹并在其中提取apk文件内容。

要执行的

命令

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

源代码:

Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();

1 个答案:

答案 0 :(得分:1)

试试这段代码:

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