关于go:在-java中调用go

28次阅读

共计 569 个字符,预计需要花费 2 分钟才能阅读完成。

use https://github.com/shangzebei…

package main
import (
    "fmt"
    "unsafe"
    "gitee.com/aifuturewell/gojni/java"
)
func main() {}
func init() {java.OnMainLoad(func(reg java.Register) {reg.WithClass("com.nk.Hello").
            BindNative("nice", "void(java.lang.String[])", nice).
            Done()})
}

func nice(ss []string) {fmt.Println(ss)
}

在 java 中

package com.nk;

public class Hello {
    static {System.loadLibrary("test");
    }
    public static void main(String[] args) {nice(new String[] {"come", "from", "gojni"});
    }
    public static native void nice(String[] sss);
}

golang build and run

go build -buildmode=c-shared -o libtest.so
LD_LIBRARY_PATH=. java com.nk.Hello

正文完
 0