Random Noise

cat /dev/random >> /dev/dsp

JNI debugging using Eclipse and Visual Studio 2005

with 2 comments

Recently I was involved in a project that involved java code accessing native c++ libraries. All went fine from creation of the JNI functions to its implementation. In one of the functions has java.nio.ByteBuffer as parameter. On the java side this buffer was initialized using allocateDirect. On the c++ side, the buffer content is altered and size gets reduced. The limit is changed using limit(int). I managed this all using cout/printfs/err to print the debug information.

I put up a screen cast (without voice) for anyone who needs to watch this method inaction. Download this video from here: http://www.hotlinkfiles.com/files/1266155_p61kq/jni-debug.divx

Short description of the same:

So, I really needed to look at the live data. Hence needed a way to debug JNI c++ code. So, as everyone else I turned to Google. Finally this article described the need for dual debuggers. I understood that we need to start the java application that uses jvm in debug mode (-Xdebug). We also need to make sure that Eclipse and Visual Studio co-operate. So we need the -Xrunjdwp jvm argument. This will specify the protocol and port address where Eclipse (remote java application) will listen to.

So, open the JNI C++ VS 2005 project and navigate to project->settings->debugging. in command specify the path to java.exe, in command arguments add this:

-Xmx400m -Xms400m -Xdebug -Xnoagent \
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 \
-Djava.library.path=”[path to debug folder containing JNI DLL]” \
-cp “.;[path to folder containing .class files for he java app]” \
[main java class]

And then set break points in the c++ code.

Then we need to create a remote java application configuration in Eclipse. Specify the port number (in this case 8000). Also in my case I had to add java source directory where Eclipse will search for the java code of the remote application. This is done from the source tab. Once done add break points where the java application loads the native library and where you call the native function.

Then start debugging the VS project (f5). The java application will be in suspended mode (because suspend=y argument) and wait for external debugger to attach. So, start the Eclipse’s remote java application configuration that we created in debug mode. And start debugging!


Written by Vivek Unune

May 18, 2007 at 11:58 pm

Posted in Java, linux, VC++

Tagged with , , , ,

2 Responses

Subscribe to comments with RSS.

  1. Works great! Except that the console output shows up neither in VC or Eclipse. Did you experience that?

    tom Kratzke

    May 14, 2009 at 7:05 pm

  2. I manage to do it.
    I launch the java program with java.exe .
    I attach the process in VS and run the remote java application in Eclipse.

    Thank you for your usefull information!

    Guillaume Valancogne

    June 4, 2009 at 12:44 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: