1

I'm trying to run a batch script on windows using groovy. I started with this code:

StringBuilder output = new StringBuilder()
StringBuilder error = new StringBuilder()
dir = new File("$homedir")
Process proc = "pathToScript\script.bat --arg test".execute(null, dir)
proc.consumeProcessOutput(output, error)
proc.waitFor()
print proc.exitValue()

This code works just fine for my scripts, but some commands don't play nice, so I decided to start using an ArrayList to store the arguments. So now my code looks like this:

StringBuilder output = new StringBuilder()
StringBuilder error = new StringBuilder()
dir = new File("$homedir")
ArrayList command = ["pathToScript\script.bat", "--arg test"]
Process proc = command.execute(null, dir)
proc.consumeProcessOutput(output, error)
proc.waitFor()
print proc.exitValue()

Now for some inexplicable reason it runs script.bat without the arguments. I've printed out the list to be sure it contains the arguments properly, which it does, so I'm not sure why the execute method isn't using them.

1 Answer 1

4

Try:

ArrayList command = ["pathToScript\script.bat", "--arg", "test"]

When an instance of List is used as source for execute() all arguments should be passed separately.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.