0

I am looking for the best approach for calling modern C++ 11/14 code from java (with callbacks from C++ back to java). What is the best/easiest/fastest performance way to get interoperability between java and C++ 11/14? I have been programming in python and C++ for a while and have come to depend upon pybind11. I am looking for something similar to (or better than) pybind11 for java.

I am aware of jni and jna. But jni is too low-level, tedious, and error prone. And according to this blog post "JNA is significantly slower than the JNI." That blog post uses bridj, but in the three years since that article was written bridj seems to have gone from being "a relatively young project" to one that has only two commits since then.

I am also aware of swig, but for python at least, I much prefer the direct wrapping that pybind11 offers over the wrapper generator approach of swig. I have come across jni.hpp and javacpp. jni.hpp seems to look the most like pybind11 but it looks to be immature and not widely used within the java community. Is javacpp the closest java/c++ equivalent to pybind11 for python/c++?

I appreciate any guidance.

(For those who closed this question as a duplicate of the over 7 year old question How to call C++ from Java?, my question is asking about the current best libary that supports java/c++ interoperability using up-to-date C++ language improvements that have taken place since 2011. Please re-open.)

Phil
  • 5,822
  • 2
  • 31
  • 60
  • 2
    Seems like off-topic request for resources to me. That aside, I like [zeromq](http://zeromq.org/) and [protocol buffers](https://developers.google.com/protocol-buffers/). – Elliott Frisch Mar 20 '19 at 20:34
  • @ElliottFrisch: OP didn't ask for a specific piece of software, but for "how to achieve X". – einpoklum Mar 20 '19 at 20:46
  • No, I am not looking for messaging. I need to make direct function calls from java to c++. I have high-performance image processing functions implemented in C++ and will write the user interface in java. The images will be owned by the jvm and pointers to the memory blocks will be passed to C++. I need all of this to be in-process, function calls. I am not looking to exchange serialized messages. If this is not the correct forum to ask this question, what is? – Phil Mar 20 '19 at 20:47
  • @einpoklum *I am looking for something similar to (or better than) pybind11 for java.* – Elliott Frisch Mar 20 '19 at 20:54
  • SWIG will allow you to make callbacks from C++ to Java. It can be complex to set up, but it will do what you want. By the way, "significantly slower" doesn't necessarily matter unless you're making many frequent calls. See also https://stackoverflow.com/questions/7593334/how-to-call-c-from-java – Andy Thomas Mar 20 '19 at 21:03
  • 1
    I use https://github.com/jnr/jnr-ffi which is faster than JNA and easier than SWIG. – Peter Lawrey Mar 20 '19 at 22:30
  • Thanks. Is there any documentation for jnr-ffi other than the source code? Have you tried javacpp? If so, how would you compare jnr-ffi with javacpp? – Phil Mar 20 '19 at 22:43
  • Are you having any issues using JavaCPP? If so, please ask that kind of question. – Samuel Audet Mar 31 '19 at 14:28
  • Not yet. From what I have seen, javacpp is the answer to my question. It seems to be the java equivalent to what pybind11 is for java. When I start using it, I will post specific questions. Thanks. – Phil Apr 01 '19 at 03:17
  • 1
    It's not like pybind11 in the sense that it doesn't force the user to do everything in C++, but it is pretty much the only tool available for Java. – Samuel Audet Apr 03 '19 at 08:54
  • 1
    Thank you for your comment. I will be using javacpp for getting interoperability between java and c++. – Phil Apr 03 '19 at 14:22

0 Answers0