0

I am trying to install an android app into the device from a Java application.

Using the following command - Runtime.getRuntime().exec("adb install /apps/testapp.apk");

I face the following error - Cannot run program "adb": error=2, No such file or directory

Should I have to use ProcessBuilder to start the command execution?

Kris_28
  • 31
  • 7
  • I suspect this is a PATH issue; have you tried using the full path to adb, i.e. `Runtime.getRuntime().exec("/path/to/androidsdk/tools/adb install /apps/testapp.apk");` ? – jonk Dec 18 '15 at 10:00
  • No need to use `adb` parameter in `exec()`. [Here](http://stackoverflow.com/questions/34104119/why-does-exec-start-a-adb-daemon) one can find an explanation why not. Use `pm install app.apk` instead. Although it's not a trivial task to execute the command programmatically. You may need to grab `su` first. – Onik Dec 18 '15 at 19:53

2 Answers2

0

I think problem may be

  1. adb environment variable is not set. you can try with full path
  2. path of the apk should be relative to java project CLASSPATH or the full path
Shettyh
  • 1,188
  • 14
  • 27
0

Use absolute file paths, use -r option to reinstall app if already installed:
Runtime.getRuntime().exec("adb install -r _HERE_AbsoluteFilePath_");
If you will be waiting on an execution:

String[] commands = new String[3];
commands[0] = "adb";
commands[1] = "install";
commands[2] = "-r";//reinstall if already installed
commands[3] = ___HERE_AbsoluteFilePath___;
Process p1 = Runtime.getRuntime().exec(commands, null);
p1.waitFor();

PS: if unable to run adb in console window or terminal - use absolute file path for adb or include path to environment variable.

Mark Martin
  • 372
  • 3
  • 8