The following code in Groovy adds GString
s to the list:
List<String> args = [ 'cmd', "-Dopt=${value}" ]
When I create a ProcessBuilder
with this list, I get a ClassCastException
. What's a groovy way to coerce the list elements to the correct type?
Groovy - add() Append the new value to the end of this List. This method has 2 different variants. boolean add(Object value) − Append the new value to the end of this List.
In Groovy, the List holds a sequence of object references. Object references in a List occupy a position in the sequence and are distinguished by an integer index. A List literal is presented as a series of objects separated by commas and enclosed in square brackets.
Or, you can do:
List<String> args = [ 'cmd', "-Dopt=${value}"] as String[]
or
List<String> args = [ 'cmd', "-Dopt=${value}"]*.toString()
actually, why are you using ProcessBuilder out of interest? Groovy adds ways to do process management, and even adds three execute
methods to List
You can do (this is on OS X or Linux):
def opt = '-a'
println( [ 'ls', "$opt" ].execute( null, new File( '/tmp' ) ).text )
which prints out the files in my /tmp
folder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With