IllegalArgumentException while running go program through Java

My Go code is as below

package main

import "C"

//export MyFunc
func MyFunc(records []string) [][]string {
   records := make([][]string, 0)
   .
   .
   .
   return records
}

I need to run this go file through Java Native Access.
Java code for same is as below-

public Class MyClass {
    public interface MyInterface extends Library {
        String[][] MyFunc(String[] records);
    }

    static{
        MyObj = (MyClass) Native.loadLibrary("/path/to/so_file", MyClass.class);
    }
    
    public static void main(String[] args) {
        String[] strArray = {"abc1|HLJHSA|hgah","abc2|ksahd|","abc3|876987","abc4"};
        String[][] records1 = MyObj.MyFunc(strArray );
    }
}

I did all steps correctly but i am getting below error

Exception in thread "main" java.lang.IllegalArgumentException: Unsupported return type class [[Ljava.lang.String; in function MyFunc
	at com.sun.jna.Function.invoke(Function.java:373)
	at com.sun.jna.Function.invoke(Function.java:223)
	at com.sun.jna.Library$Handler.invoke(Library.java:204)

I also tried with
ArrayList<ArrayList> MyFunc(String[] records); and
List<List> MyFunc(String[] records);

Please Help me to find correct equivalent for make([][]string, 0) in java

Iā€™m pretty sure you cannot do this. You can pass a pointer to a Go structure to cgo/the JNI, but you cannot pass a pointer to a structure that itself contains pointers. A slice of slices contains pointers to pointers. I would recommend serializing the data in some format (e.g. would JSON work for you?) to pass it to Java, perform some work, serialize the response, and then return it to Go.

1 Like