Using java (jar) libraries in go

2,192 views
Skip to first unread message

dpapathanasiou

unread,
Dec 6, 2015, 1:37:22 PM12/6/15
to golang-nuts
I'd like to use Titan Database[1] with go, but I could not find any
pre-existing drivers.

Titan is written in java, and is normally built with maven, producing
a jar library file[2] used by jvm languages.

If I wanted to write my own, what is the recommended approach?

Is there an equivalent of cgo for java jars?

[1] https://thinkaurelius.github.io/titan/
[2] https://github.com/thinkaurelius/titan/blob/titan10/BUILDING.md

Justin Israel

unread,
Dec 6, 2015, 4:29:43 PM12/6/15
to dpapathanasiou, golang-nuts
If you goal is just to communicate with a Titan Server, then I think all you need is a gremlin client, since its an abstraction layer over swappable graph database backends. I've not used this library, but a quick search shows a recent project for a gremlin client:



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dpapathanasiou

unread,
Dec 6, 2015, 5:59:33 PM12/6/15
to golang-nuts, denis.pap...@gmail.com
Thanks for the suggestion, that's a good idea.

I was thinking more in terms of a direct connection, though, especially
for smaller deployments where I don't have a full titan server running.

I did find this project, which takes advantage of the Java Native
Interface to use a C bridge, and finally cgo:

https://github.com/timob/jag

Owen Tuz

unread,
Dec 6, 2015, 10:09:58 PM12/6/15
to golang-nuts
This is summed up on StackOverflow fairly well:
https://stackoverflow.com/questions/23872323/how-to-use-jar-file-in-golang

The short version is that the "simplest" (you'll see why that's in quotes in a second) approach would be to use cgo to call some C code, and use C's more tried-and-tested Java bindings to then call your Java library via the JNI.

For an example, you could look at this, sadly abandoned, Github repo where someone has done exactly that:
https://github.com/abneptis/GoJVM

I wouldn't really recommend it, however. It may be simpler to find another way to connect, or even to write your own drivers.

Cheers,

Owen

Tim O'Brien

unread,
Dec 7, 2015, 9:43:03 AM12/7/15
to golang-nuts, denis.pap...@gmail.com
Hi,

I'm the author of Jag, if you have relatively simple API on the Java side, it will generate a nice Go package to access it.

The Cgo code that uses JNI is in https://github.com/timob/gojvm. It is a fork from https://github.com/abneptis/GoJVM, it's in desperate need of some love, its old and crusty. I am taking another look at it with Go 1.5 Cgo changes, to see whether I should replace it, it won't effect the API of the package Jag generates.

Tim

dpapathanasiou

unread,
Dec 8, 2015, 7:56:25 PM12/8/15
to golang-nuts
Thanks for that link, as well as the link to GoJVM; I'll study the source.

dpapathanasiou

unread,
Dec 8, 2015, 7:57:03 PM12/8/15
to golang-nuts, denis.pap...@gmail.com
Thanks! I'd be happy to start making pull requests once I wrap my mind around the concept.
Reply all
Reply to author
Forward
0 new messages