Thursday, May 5, 2011

how to call java function from c++?

Hello all how can i call java function from c++ application ( not using cmd methods.. ) Thanks

From stackoverflow
  • Read up on JNI.

  • Check out the JNI Invocation interface. This will allow you to embed a JVM within your C (or C++) application.

    Note that various easier mechanisms exist to facilitate calling C/C++ from Java (e.g. JNA). It may be worth considering inverting your problem such that you can call from Java (I understand this may well not be possible for your particular application, however)

  • As an example, check Creating a JVM from C. It shows a sample procedure to create a JVM and invoke a method. If the JVM already exists; e.g. your C program is invoked by the Java program (callback situation), you can cache the JNIEnv* pointer.

    As an advice, be careful caching pointers to the JVM from C/C++, there are some semantics involved as to what you can cache and it could be invoked later on. For that, as Brian Agnew pointed out, check the JNI reference.

  • Sun has a very good and basic example of JNI.

0 comments:

Post a Comment