Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug native jni c++ code in eclipse with java project

I have a Java Project in which I used Native Library (dll) whose code is written in CPP. I want to debug the java application and as soon as I reach to a point from where the native method compiled in dll called I want to step into the CPP code. How should I do this? Is there Any IDE available that can switch debugging between java and cpp code?

like image 842
rohit roy Avatar asked Jan 29 '16 09:01

rohit roy


People also ask

How will you create a native method header?

Generate a header file for the native method using javah with the native interface flag -jni . Once you've generated the header file you have the formal signature for your native method. Write the implementation of the native method in the programming language of your choice, such as C or C++.


2 Answers

Don't know of any debugger that can actually switch but you can attach two debuggers to your process. Starting your Java program in your Java IDE in debug mode attaches the Java debugger. Then open the IDE you use for your CPP code and attach its debugger to the running Java process.

In Visual Studio this would be Debug -> Attach to Process.... You get a dialog with a process list. Choose your Java process here. You might have to switch the field just above the list to "Native code" if Visual Studio can't detect that correctly. Set an appropriate breakpoint in your CPP code and the debugger will stop there. Make sure your Java process loads the debug version of the library for this to work.

like image 53
user2543253 Avatar answered Sep 23 '22 13:09

user2543253


This also works for Eclipse: start debugging the "enveloping Java application" that invokes the .dll (or .so, on Linux), set a breakpoint at the accessory native call and start second debugger from the native process that is started there.... Result is: 2 debuggers, one running in Java and the second in the native .dll/.so... when the latter is finished and returns to Java again, it should be possible to resume Java debugging from there. In my environment, this setup works.

like image 40
SoHerman Avatar answered Sep 25 '22 13:09

SoHerman