Working with both C and Java in the same project brings its own particular challenges which are often overcome in a number of interesting manners many of which prove to be unworkable at larger scale
Debug a single Java maven project that contains both java and JNI code
- Create a new Eclipse project using "File -> Import -> Existing Maven Projects"
- Right click on the project "New -> Other" "C/C++ -> Convert to a C++Project (Adds C/C++ Nature)" (I choose the Linux toolchain)
- Go to the "C/C++ Perspective"
- I add the folders containing the c++ source to the build path so I can set break points
- Ensure you can see "/usr/includes" under the "Includes" element of the project tree
- Put some breakpoints in your code:
- Put a breakpoint in the Java code before your C code starts (for example imeadiately after the System.loadLibrary call (you must ensure that this Run Configuration has its source tab including the C code you wish to debug)
- Put a breakpoint in the C code wherever you like
- Create a Eclipse "Debug Configuration" for the wrapping Java process (I have attached my config)
- Under "Arguments -> VM arguments", set the -Djava.library.path=/home/tom/projects/jbosstm/blacktie/integration-tests/target/ (where this is the path to your .so containing JNI)
- Under "Environment" I set LD_LIBRARY_PATH to /home/tom/projects/jbosstm/blacktie/integration-tests/target/cxx/test/lib/ (path for any dependencies of your JNI .so)
- Create a new Eclipse "C/C++ Attach to a Local Application" Debug Configuration within one of your Eclipse C++ projects
- Point the "C/C++ application" at the actual java executable that your Java debug configuration process launches (for example /usr/lib/jvm/java-1.6.0-openjdk-184.108.40.206.x86_64/bin/java), no other changes should be required (for example ps -eaf)
- Start the Java application in the Eclipse Java Debugger
- When the java breakpoint fires, start the "C++ Attach to a Local Application" debug configuration
- You will be asked to connect to a process, choose the correct "java" one, for example, filter the processes by "java"
- Eclipse will stop the GDB process at one of its own breakpoints (which I can't seem to override), you need to press Play (F8) on the GDB debugger to move this on
- Watch in awe as both your java and C breakpoints are hit
Thats all there is to it! Hope this has helped!