(Tutorial) Debugging C++ Code from Java Application By Gregory Shpitalnik
Tutorial: Debugging C++ Code from Java Application
By Gregory Shpitalnik
How to debug simultaneously Java/C++ mixed code in both Java and C++ debuggers
Introduction
There are a wide range of applications that consist of two parts. The main GUI part is written in Java and the performance oriented part is written in C++. The GUI part reacts to the user inputs and passes its requests to the C++ part that usually implements the application core functionality. During application development, one can debug the Java GUI part from within Java debugger, for example, in Eclipse IDE. However the cooperative Java and C++ parts debugging during the Java application execution is not that simple. Usually the C++ part is debugged separately from the Java part in the C++ debugger by writing additional C++ test code that invokes the C++ part APIs. This is very inconvenient and quite a non-productive approach. It would be better if we could be able to debug the application code simultaneously in both the Java debugger and the C++ debugger while the same application runs.
In this article, I will show a simple solution that allows invocation of C++ debugger from Java debugger whenever the application flow reaches the C++ code in Unix/Linux environment.
Java and C++ Interaction
Java allows invocation of C/C++ (and other languages) APIs from Java code by using the so called Java Native Interface (JNI). In JNI, native functions are implemented in separate *.c or *.cpp files. (C++ provides a slightly cleaner interface with JNI.). When the JVM invokes the native function, it passes a JNIEnv pointer, a jobject pointer, and any Java arguments declared by the Java method. The details of JNI interface are beyond the scope of this article. For our purposes, it is just important to mention that every C/C++ function/method that you want to call from Java must be wrapped by C function (JNI wrapper) that will be called directly from Java and will in turn invoke the required C/C++ API.
| Read More..
Courtesy: Codeproject.com
- guru's blog
- Login or register to post comments

