Two dimensional ruby array to java array

I’m trying to convert a list of lists of strings in jruby to a
properly typed java array to send to a java function. I’m familiar
with the .to_java method on lists, but I’m not sure how to properly
type everything.

foo = [[“a”, “b”, “c”], [“d”, “e”, “f”]]

bar = foo.to_java(???) <---- what goes here

obj.setData(bar)

// in java
public void setData(String[][] args) { … }

What should I pass to to_java so I can call setData without getting a
type error?

Seems this is a recurrent issue, we should clarify it somehow, meanwhile
this spec is plenty of examples:

https://github.com/jruby/jruby/blob/master/spec/java_integration/types/array_spec.rb

As a summary, argument symbols for the method to_java are aliases for
real
java classes, so :string is an alias for java.lang.String or :long is an
alias for the primitive type long, but there aren’t aliases for arrays,
so
we need to use those java classes directly with the syntax
Java::class_name.
So, to convert an array to a java array we can use this syntax:

.to_java(Java::java.lang.String[])

https://github.com/jruby/jruby/blob/master/spec/java_integration/types/array_spec.rb
Cheers

I just wrote a small guide about this in my blog:

http://is.gd/jHglu

I hope it will be useful.

Cheers

On Wed, Dec 29, 2010 at 10:48 AM, David C.

That’s exactly what I needed. Thank you for the blog post.

On Wed, Dec 29, 2010 at 3:44 AM, David C.