Skip to content
/ gojni Public
forked from shangzebei/gojni

golang与java之间相互调用(支持Android,JAVA后端)

License

Notifications You must be signed in to change notification settings

ly269nva/gojni

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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}}

About

golang与java之间相互调用(支持Android,JAVA后端)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 63.4%
  • C++ 35.8%
  • C 0.8%