3

I want a java program to execute the following shell command:

apktool.jar d /path/to/my/app.apk

This command perfectly works when executing it directly on command line.

Java Code:

public static void main(String[] args) { String command = "apktool d /path/to/my/app.apk"; try { Runtime.getRuntime().exec(command); } catch (IOException e1) { e1.printStackTrace(); } } 

There is no error, no exception. Nothing happens and i have the impression that I already searched the entire internet for a solution. Does anybody know what I am doing wrong? A simple command like

mkdir /path/to/a/new/folder

works without problems.

I tried the same using ProcessBuilder:

try { Process process = new ProcessBuilder(command).start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

This time i only get "Cannot run program "apktool d /path/to/my/app.apk, No such file or directory". I can't even run the mkdir command.

2 Answers 2

2

You need to call the jar with java.exe, and you're not doing that. Also you need to trap the input and error streams from the process, something you can't do the way you're running this. Use ProcessBuilder instead, get your streams and then run the process.

For example (and I can only do a Windows example),

import java.io.IOException; import java.io.InputStream; import java.util.Scanner; public class ProcessEg { private static Process p; public static void main(String[] args) { String[] commands = {"cmd", "/c", "dir"}; ProcessBuilder pBuilder = new ProcessBuilder(commands); pBuilder.redirectErrorStream(); try { p = pBuilder.start(); InputStream in = p.getInputStream(); final Scanner scanner = new Scanner(in); new Thread(new Runnable() { public void run() { while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } scanner.close(); } }).start(); } catch (IOException e) { e.printStackTrace(); } try { int result = p.waitFor(); p.destroy(); System.out.println("exit result: " + result); } catch (InterruptedException e) { e.printStackTrace(); } } } 
Sign up to request clarification or add additional context in comments.

7 Comments

sorry, i edited my post. apktool is a .sh file that calls apktool.jar.
@null: Same idea -- then run it with the sh command (I believe as I don't do unix).
I ran it with the sh command, but nothing happens. I updated my question, because i can't even run mkdir using ProcessBuilder.
@null: again, I don't see you capturing streams anywhere. Please read When Runtime.exec() won't.
Your code works, but only after exporting the project to a .jar file. When trying to run the code in eclipse, it terminates with result code: 1. I don't understand it, because I can run simple calls like apktool -version without problems in eclipse. Do you have an explanation (maybe some permission problems)?
|
0

Try doing it like this:

 StringBuffer output = new StringBuffer(); Process p; try { p = Runtime.getRuntime().exec("./path/apktool d /path/to/my/app.apk"); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine())!= null) { output.append(line + "\n"); } } catch (Exception e) { e.printStackTrace(); } system.out.println(output.toString()); 

Creating first a process allows you to wait for a response and reads the output of the execution of your process.

If something is failing while running your shell command, you will have the error printed at the end.

Also, make sure your java program can access your shell script, or better provide the full path to it like:

./path/to/shell/apktool d /path/to/my/app.apk 

4 Comments

FYI: waitFor() is a blocking call.
So?, I already know that, that's exactly what I'm telling him to do, wait for the execution of "apktool" to finish, this way he can get the response and see if something failed.
Don't forget that Linux does not add the current folder to the path by default. You need './' to preceed any invocation. p = Runtime.getRuntime().exec("./apktool.sh d /path/to/my/app.apk");
I get the same error as before: cannot run program error=2 no such file or directory. Apparantly this error is well known, as there are many comments in the internet, but till now there no solution for me...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.