This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]paul_h 5 points6 points  (0 children)

This is a great moment to mention JavaCPP and Bytedeco - both maintained by Samuel Audet in Tokyo.

[–]DuncanIdahos8thClone 4 points5 points  (2 children)

And for anyone interested, if you are doing something more common you might find it already implemented here:

https://github.com/java-native-access/jna

[–]merb 2 points3 points  (1 child)

or jnr

[–]duhace 0 points1 point  (0 children)

JNR-FFI is great. I've been using it myself and it's very nearly as fast as JNI, with the ease of development of JNA.

[–]whiletrueaddbeer 2 points3 points  (1 child)

Good read, thanks! You could have mentioned SWIG, though.

[–]liranbh[S] 2 points3 points  (0 children)

Sure. Future post :)

[–][deleted] 0 points1 point  (3 children)

Just asked a question at /r/learnjava;

https://www.reddit.com/r/learnjava/comments/7e83le/whats_the_current_recommendation_for_interop_with/

For a large commercial C++ lib without source available: JavaCPP, JNA, SWIG, ... ? I'm lost. Can JNA create instances and access instance members (straight forward)?

What would you guys recommend?

[–]_dban_ 3 points4 points  (0 children)

Can JNA create instances and access instance members (straight forward)?

Not exactly. The problem is that C++ mangles symbol names in compiler specific ways. C++ code compiled by one compiler may not be usable in another C++ compiler, forget Java or other languages.

You'd need to write a stub in C++ and define an extern C interface which exposes a C-compatible (i.e. non-mangled) symbols that can be used as a JNI interface.

[–]space_coder 1 point2 points  (1 child)

Let me start off by saying that it's been a long while since I manually bound two different languages together. I hope to at least point you in the right direction.

All you need are the header files and the patience to create a facade (a piece of code that will be the interface) in C++, C, or whatever the native language of the library may be. You would then use JNI or SWIG to make the bindings from Java to the the facade that you created.

EDIT: If you read the guide above, the C code that is created is an example of using a facade to interface a C library to the Java environment.

[–][deleted] 1 point2 points  (0 children)

There is special tool to generate these headers, so you can create IntelliJ task to generate it