60

I'm trying to call a methond on a Java class from a Groovy class. The Java method has a String array as a parameter, and I have a collection of Strings in my Groovy class. How do I convert the Groovy collection to a Java String array?

Java Method:

public class SomeJavaClass{
  public void helpDoSomething(String[] stuff){

  }
}

Groovy code

class SomeGroovyClass {
  def data = ["a", "b", "c"]

  def doSomething = {
    def javaClass = new SomeJavaClass()
    javaClass(data) //Groovy passes ArrayList, Java class expects String[] ???
  }
}

1 Answer 1

115

To be correct, def data = ["a","b","c"] it is a List, not an array.

Just try casting like this:

def data = ["a","b","c"] as String[]
Sign up to request clarification or add additional context in comments.

1 Comment

I knew there was something groovier than (String[])data.toArray(new String[data.size]) Thanks!!!

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.