Samuel Lampa
Samuel Lampa

Reputation: 4392

How to call java code from Go without invoking the JVM for every call?

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?

Upvotes: 8

Views: 12872

Answers (3)

Jman
Jman

Reputation: 91

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.

Upvotes: 9

skyde
skyde

Reputation: 2956

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.

Upvotes: 5

Vitruvie
Vitruvie

Reputation: 2337

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.

Upvotes: 0

Related Questions