9 Star 37 Fork 0

aifuturewell / gojni

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or Download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.MD

quick start

1.java invoke native

in go

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

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

in 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);
}

build and run

CGO_CFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/linux" go build -buildmode=c-shared -o libtest.so
LD_LIBRARY_PATH=. java com.nk.Hello

or

go env -w CGO_CFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/$(go env GOHOSTOS) -g -O2"

2.native invoke java

native.LoadClass("com.szb.Jvm").New().Invoke("attach", "void()")

fmt.Println(vm.RunSource("java.lang.Thread.currentThread[java.lang.Thread()]();").
				AsObject().
				Invoke("getName", "java.lang.String()").
				AsString())
{{$s=new java.lang.String}}

Repository Comments ( 1 )

Sign in for post a comment

About

golang与java之间相互调用 expand collapse
Go
MIT
Cancel

Releases

No release

gojni

Contributors

All

Activities

Load More
can not load any more
1
https://git.oschina.net/aifuturewell/gojni.git
git@git.oschina.net:aifuturewell/gojni.git
aifuturewell
gojni
gojni
master

Search