Is it possible (and if so, what is the recommended way) to call java code from Go, without the need to start the JVM for every function call?
I.e, is there any equivalent to the jpype solution for python, which lets you start the JVM once, and then import java classes and call them, using the already started up JVM?
You can't run Java program without JVM. JVM is responsible in running a Java program, but the only file that can be executed by JVM is Java bytecode, a compiled Java source code.
You can make a Java management class in Java which is capable of talking to your Go program, and which you run once and will make the appropriate calls to other Java code when your Go program asks for them.
By compiling their Go packages into C-style shared libraries, Go programmers can easily make their projects work with C, Python, Ruby, Node, Java, etc. using in-process integration of shared object binaries.
Use cgo to call into C code that creates a JVM instance using the JNI invocation API, and call into Java code using the JNI interface. As goroutines can technically switch between native threads, you'll probably have to be very careful about testing, attaching and detaching threads to the JVM upon entry and exit from Go code and/or supplement with use of a native threads library like pthreads.
Bundle your java code in a "server" and call it with RPC like "rest/soap/thrift" and keep the server running. I don't know of any system that automate this for you though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With