Pman's Lab

Just another IT lab

2

Gradle Exec: a problem occurred starting process command

Recently I started working with Gradle and I’m learning more about it as I’m working on real use cases. While working on a simple task (using a Windows machine), I came across an error message and obviously I turned to the internet to find out the fix/root cause.  I did come across a few resources reporting same error message, but they didn’t seem very useful to me. Eventually I was able to solve the issue and I wanted to document it here in case it helps someone else.

To start an external program or simply run a command on the machine from gradle script, normally you’d configure an Exec type task. For example, let’s say I have the following piece of code in build.gradle file.

task ipconfig(type: Exec) {
    commandLine 'ipconfig'
}

Now, when I run ‘gradle ipconfig’, command prompt will echo the same output of the ‘ipconfig’ command as if it was ran manually from a command line.

Let’s forget about gradle for a moment and create a simple app. The app will write/echo “Hello world!”. First I will create a new directory on C: drive called “myapp”. In this directory, I create a simple batch script file called “myapp.bat” with following commands:

@echo off
echo "Hello World!"

From a Command Prompt, if I browse to “C:\myapp” directory and run this batch script, it should write “Hello World!” on the command prompt. Let’s take this one step further and add “C:\myapp” to the machine’s environment PATH variable. This will allow us to run our batch script from ANY location.

Now we have a really simple app (command line based). Open a command prompt, type “myapp” and press Enter. You’ll receive “Hello World!” printed/echoed on the screen.

Let’s try to call this app from our gradle script. Add another task to our build.gradle file that will run our new app/script. Here’s our new task:

task myapp(type: Exec) {
    commandLine 'myapp'
}

Looks very similar to our previous task. I’m simply expecting to run “myapp”, which we know for sure works through a Command Prompt. If I run “gradle myapp” from the gradle script directory, the script failed with an error message as shown below:

:myapp FAILED
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':myapp'.
> A problem occurred starting process 'command 'myapp''

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

If I run “gradle myapp –stacktrace”, I get following details about the failure

Caused by: java.io.IOException: Cannot run program "myapp" (in directory "C:\temp\gradleTests\Exec"): 
CreateProcess error=2, The system cannot find the file specified.
        at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
        ... 4 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified.
        ... 5 more

It seems Gradle cannot run the program because it cannot find it. Even though I added the program/script’s location to the environment path variable. Let’s modify gradle script. I will tell it exactly where to find it.

task myapp(type: Exec) {
    commandLine 'myapp'
    workingDir 'c:\\myapp'
}

I added the “workingDir” property to the task’s configuration where the script “myapp” is located. Now if I run “gradle myapp”, I still have the exact same error message (A problem occurred starting process command). By running the task with –info or –stacktrace, I can confirm that Gradle is looking in that directory, but it’s still not able to find “myapp”.

There’s only one thing left to try:

task myapp(type: Exec) {
    commandLine 'myapp.bat'
}

Run that task now and Gradle is able to call that script successfully… from any location, as I was expecting in the first place. Obvious difference is the script’s extension being specified in task’s configuration, where as I didn’t need to do that with the task “ipconfig”, which is also an actual program in Windows named “ipconfig.exe”. So, why is Gradle having problem finding the command in this case? It could be due to PATH environment variable not being configured properly, but if that were the case, then I wouldn’t be able to manually run “myapp” from command line from any random directories.

Anyways, if you do run into an issue like this, try specifying the full name of the program/script.

Please note that this test was done on Windows 2008 R2 (64-bit) machine with following configuration:

------------------------------------------------------------
Gradle 2.3
------------------------------------------------------------

Build time:   2015-02-16 05:09:33 UTC
Build number: none
Revision:     586be72bf6e3df1ee7676d1f2a3afd9157341274

Groovy:       2.3.9
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.7.0 (IBM Corporation 2.7)
OS:           Windows Server 2008 R2 6.1 amd64
errorexec taskgradle script

pman • March 23, 2015


Previous Post

Comments

  1. h May 17, 2016 - 3:07 am Reply

    It workS, thanks!

  2. Guna August 19, 2016 - 9:38 am Reply

    Works like a charm!!! Thank you very much!!
    Adding .bat did make the commandLine work correctly

Leave a Reply

Your email address will not be published / Required fields are marked *


+ 4 = seven

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>