Calling C++ functions from Java

14,702

Solution 1

You need to define native methods in your java code for whatever you want to be implemented in C++ and directly access your native code. Then you run javah on your code and it will generate the C header files for you and you'll need to provide C++ implementations.

The native methods you can call from your Java code like any other methods and still they'll have their implementation written in C++ and talking to whatever other native library directly.

You then need to set the java.library.path system property to include the shared C/C++ libraries that you require: the google library and your own JNI implementation library would be required in this case.

Solution 2

If the library has C bindings through a DLL/SO, I usually prefer writing wrappers in Java using Java Native Access (JNA) rather than writing the bindings in C/C++ using the Java Native Interface (JNI). The former is easier to manipulate as the JNI access to Java objects is a real pain in the neck. However, it's not as obvious to wrap C++ classes using that API.

You might also want to look into the Simplified Wrapper and Interface Generator (SWIG) for automating part of this process!

Solution 3

You can't run native code on App Engine - only JRE code. If there's no avoiding the native code, you'll need to run this part of your app on another system, and call it from your App Engine app - or use the built-in XMPP API, in this case.

Share:
14,702

Related videos on Youtube

Namit Sinha
Author by

Namit Sinha

Trying to build something that will live after i cast into void http://www.quora.com/Namit-Sinha

Updated on June 04, 2022

Comments

  • Namit Sinha
    Namit Sinha almost 2 years

    I am developing a Java application in which I need to call some C++ functions (from Google Talk library libjingle) . The objective is to run it all on Google App Engine (which only supports Python or Java).

    How can I do this?

    • Karl von Moor
      Karl von Moor about 13 years
      With JNI, Why dou you already have that in your tags ?!
    • Asaf
      Asaf about 13 years
      do you specifically require libjingle or will any XMPP library do? you can see a list of available libraries here
  • Admin
    Admin about 13 years
    Thank you all. Do you know if JNI and native code in C++ work fine with Google App Engine?
  • ryan
    ryan about 13 years
    no, they don't. app engine doesn't support native code or compiled binaries, via JNI or anything else. From that link: "An app cannot provide or directly invoke any native JNI code." asaf's comment above is on the right track. you probably want to use app engine's built in xmpp api instead.

Related