With the help of JNA, we can call functions defined by GO from Java.
cd go
go build -buildmode=c-shared -o awesome.so awesome.go
If succeed, you will see awesome.h
and awesome.so
generated, both of them are very important. awesome.h
is used to read the definition of the generated functions and you can define the corresponding structure in Java code. awesome.so
is used for calling.
If there is any message shown in the console, there probably be something wrong.
Then run Hello.java
(from IDE)
- In go file, we must declared
import "C"
. Since IDEA will auto remove unused imported lib, we have to use//// formatter: off/on
around it to keep it. Also note we have to use////
instead of//
to makego build
work - In go file, we must use
//export MyFuncName
to export the functions we want to call from Java. Note it must be//export
, there is no spaces between//
andexport
. - In go file, the package must be
main
, and there is a functionmain
- We can use
nm awesome.so | grep Multiply
to verify if the required function is exported - We can't return or pass
string
from Go code, instead, we should use*C.char
- We can't return or pass
byte[]
from Go code, instead, we should useunsafe.Pointer
You must be very careful about the advices here.