使用Native Library从Golang返回一个映射到Java,通过传递字符串作为Java的输入。

问题描述 投票:0回答:1

我想从一个Java程序中调用Golang函数。参考文献 正在使用。而这一切都很好。

有了这个,我试图从golang返回一个map到java。在Java中的返回类型是ObjectMap时,什么都没有工作。事实上,异常(也许是编译时)是。

Exception in thread "main" java.lang.IllegalArgumentException: Unsupported return type interface java.util.Map in function GetGeoLoc
        at com.sun.jna.Function.invoke(Function.java:490)
        at com.sun.jna.Function.invoke(Function.java:360)
        at com.sun.jna.Library$Handler.invoke(Library.java:244)
        at com.sun.proxy.$Proxy0.GetGeoLoc(Unknown Source)
        at com.java.readers.GetGeoLoc2Tests.getGeoLoc2(GetGeoLoc2Tests.java:39)
        at com.java.readers.GetGeoLoc2Tests.main(GetGeoLoc2Tests.java:52)

谁能帮我解决这个问题?我相信我在这里遗漏了什么。

下面是代码

Java。

public class GetGeoLoc2Tests {

    static GoCaller GO_CQM_TRANSFORMER;

    public interface GoCaller extends Library {
        public Map<String, String> GetGeoLoc(GoString.ByValue ip);
        public int Add(int i);
        public String SString(GoString.ByValue s);

        public class GoString extends Structure {
            public static class ByValue extends GoString implements Structure.ByValue {
            }

            public String ip;
            public long n;

            protected List getFieldOrder() {
                return Arrays.asList(new String[] { "ip", "n" });
            }
        }
    }

    public static void getGeoLoc2(String ip) {

        if (ip == null) {
            return;
        }

        GoCaller.GoString.ByValue str = new GoCaller.GoString.ByValue();
        str.ip = ip;
        str.n = str.ip.length();

        System.out.println("JAVA----->" + ip);
        Map<String, String> map = GO_CQM_TRANSFORMER.GetGeoLoc(str);
        System.out.println(map);
    }

    public static void main(String[] args) {
        if(args.length == 0) {
            System.out.println("Expecting an input<IpAddress>");
            System.exit(0);
        }

        String pwd = System.getProperty("user.home");
        String lib = pwd + "/go/src/ip2loc/iploc.so";
        GO_CQM_TRANSFORMER = (GoCaller) Native.loadLibrary(lib, GoCaller.class);
//      System.out.println(GO_CQM_TRANSFORMER.Add(10));
        getGeoLoc2(args[0]);
    }

Golang:

package main

import "C"

import (
        "fmt"
        "github.com/ip2location/ip2location-go"
)

func main(){}

//export GetGeoLoc
func GetGeoLoc(ip string) string {
        ip2location.Open("/data/md0/ip2loc/ip6/IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN")
        //ip1 := "8.8.8.8"
        fmt.Println("input:" , ip)
        results := ip2location.Get_all(ip)

        m := make(map[string]string)
        m["country_short"] = results.Country_short
        m["country_long"] = results.Country_long
        m["region"] = results.Region
        m["city"] = results.City
        m["isp"] = results.Isp
        m["latitude"] = fmt.Sprintf("%f", results.Latitude)
        m["longitude"] = fmt.Sprintf("%f", results.Longitude)
        m["domain"] = results.Domain
        m["zipcode"] = results.Zipcode
        m["timezone"] = results.Timezone
        m["netspeed"] = results.Netspeed
        m["iddcode"] = results.Iddcode
        m["areacode"] = results.Areacode
        m["weatherstationcode"] = results.Weatherstationcode
        m["weatherstationaname"] = results.Weatherstationname
        m["mcc"] = results.Mcc
        m["mnc"] = results.Mnc
        m["mobilebrand"] = results.Mobilebrand
        m["elevation"] = fmt.Sprintf("%f", results.Elevation)
        m["usagetype"] = results.Usagetype

        fmt.Println("map:", m)
        ip2location.Close()
        return m

在执行时,打印输出'm',这是一个地图。问题是我如何返回这个 map m 到一个Java方法,作为一个 HashMap? 谢谢你

java go java-native-interface
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.