writing java wrappers for c++

dpalyca755 used Ask the Experts™
Can someone point me to some examples/documentation of how to write Java wrappers for C++?
I need to interface with a JAVA API, from a C++ code base.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
One way of accessing Java code through C++ is by compiling the Java as native byte code using gcc-java.

Take a look at the source code for pdftk - http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk-1.44-src.zip

It is written using C++ accessing the iText java library to do the guts of its work.

JNI is the technology you'll be using to create a Java to C++ interface (it stands for Java Native Interface - in the sense that C++ code is "native" code rather than managed by the JVM), but you don't want to build this from scratch unless you're only talking about a very small C++ interface (like one class or 10 methods or something like that).

If you need to build a Java interface to a significant C++ API then you should use SWIG (http://www.swig.org/projects.html).  This is a toolkit designed to generate interfaces for a wide range of languages and Java in particular is supported.  Incidentally once you set up the SWIG tools you get a bunch of other bindings to other languages pretty much for free - if you happen to want them.

I was part of a project a few years ago that needed to create a Java interface to a substantial project written in C++ and SWIG worked really well for us.


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial