Note that there are some explanatory texts on larger screens.

plurals
  1. PO"adb start-server", Java, Gradle and apache-commons-exec: how to make it right?
    text
    copied!<p>I am running into various problems when trying to run <code>android start-server</code> from within Java as external process. The Java is called by Gradle. Let me describe to you what is exactly happening in various scenarios:</p> <p><strong>Environment</strong></p> <ul> <li>Windows 7 X64</li> <li>Java 7</li> <li>commons-exec-1.1</li> <li>Gradle 1.6</li> <li>Android API 17</li> <li>IntelliJ IDEA 12.1.4 Community Edition</li> </ul> <p><strong>Assumption</strong><br> adb daemon is killed and will start up when calling <code>adb start-server</code>.</p> <h2>Case 1</h2> <p>This code:</p> <pre class="lang-java prettyprint-override"><code>DefaultExecutor executor = new org.apache.commons.exec.DefaultExecutor(); executor.execute(org.apache.commons.exec.CommandLine.parse("adb start-server")); log.info("Checkpoint!"); </code></pre> <p>When run from Gradle <code>run</code> task of the application plugin, will display the start-server output, i.e.:</p> <pre><code>* daemon not running. starting it now on port 5037 * * daemon started successfully * </code></pre> <p>and then it will hang, i.e. "Checkpoint!" will never be logged. Killing the <code>adb.exe</code> process manually will cause the code to continue execution.</p> <h2>Question 1</h2> <p>Why this call blocks? When <code>adb start-server</code> command is run from terminal, after a couple of seconds the control is returned to the terminal, so why it doesn't happen in the code?</p> <h2>Case 2</h2> <p>If instead I use directly the Java runtime like so:</p> <pre class="lang-java prettyprint-override"><code>Runtime.getRuntime().exec(new String[]{"adb", "start-server"}); log.info("Checkpoint!"); System.exit(0); </code></pre> <p>If calling from Gradle as previously, the "Checkpoint!" will be logged. However, the execution will hang on <code>System.exit(0)</code>. Killing <code>adb.exe</code> manually will again make Gradle call finish.</p> <p>In this case no output of <code>adb start-server</code> is displayed.</p> <p>Interesting thing is that when, instead of Gradle, I run the application from IntelliJ IDEA with build setup mimicking that of Gradle's, everything works fine and the application finishes properly.</p> <h2>Question 2</h2> <p>Why Gradle hangs on <code>System.exit(0)</code> and IntelliJ doesn't? Is this somehow related to the fact that Gradle itself is a process that internally calls Java and in case of IntelliJ, Java is called immediately without any indirection? Why does it matter?</p> <h2>Question 3</h2> <p>Ultimately, I want to be able to run this from Gradle without hangs of any kind. Logging output of <code>adb start-server</code> would be a bonus. I would greatly appreciate any hints how to do this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload