Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use execute() in groovy to run any command

Tags:

groovy

ant

I usually build my project using these two commands from command line (dos)

G:\> cd c:
C:\> cd c:\my\directory\where\ant\exists
C:\my\directory\where\ant\exists> ant -Mysystem
...
.....
build successful

What If I want to do the above from groovy instead? groovy has execute() method but following does not work for me:

def cd_command = "cd c:"
def proc = cd_command.execute()
proc.waitFor()

it gives error:

Caught: java.io.IOException: Cannot run program "cd": CreateProcess error=2, The
 system cannot find the file specified
        at ant_groovy.run(ant_groovy.groovy:2)
like image 212
drake Avatar asked Mar 11 '10 18:03

drake


2 Answers

Or more explicitly, I think binil's solution should read

"your command".execute(null, new File("/the/dir/which/you/want/to/run/it/from"))
like image 155
Noel Evans Avatar answered Oct 29 '22 05:10

Noel Evans


According to this thread (the 2nd part), "cd c:".execute() tries to run a program called cd which is not a program but a built-in shell command.

The workaround would be to change directory as below (not tested):

System.setProperty("user.dir", "c:")

like image 23
ccheneson Avatar answered Oct 29 '22 07:10

ccheneson