Debugging Java Native Interface (JNI) DLLs using Microsoft Visual C++ 2008 Express and NetBeans 6.9

(This is a pretty old writeup, but it likely applies to newer versions of both development environments.)

Sometimes your JNI DLLs will crash, leaving you with the fun task of figuring out why. If you have access to the native source code, this proves to be pretty easy.

First, you need to create the Java application and JNI DLL in the usual manner. The process of creating the source code for the JNI is not covered here, but the project files for the examples used to create the screenshots can be obtained by downloading the following zip file.

test-jni-example-files.zip [see contents]

In your NetBeans project directory, you will notice that the dist directory containing the Java application also contains the JNI DLL and Program Debug Database (pdb) file, which is needed to map errors to the corresponding source code. You have to copy these two files by hand to the dist directory after building them in VS2008 Express. In a real environment, these would hopefully be built by continuous integration.

A Windows Explorer window open to the F:\test-jni-java\dist folder, which contains a lib folder, README.TXT, test-jni-c.dll, test-jni-c.pdb, and test-jni-java.jar files

In NetBeans, run the Java application. The following should appear:

A Java graphical user interface with two big buttons: Test Good JNI Function and Test Bad JNI Function

In VS2008 Express, with the DLL project open, go to the Debug -> Attach to Process menu option and attach to the running copy of the Java application.

A Visual C++ 2008 Express Edition development window with the org_vilimpoc_Jni.cpp file selected, which has code for the good JNI function and code for the bad JNI function

A debugger Attach to Process window with a list of processes and the java.exe process selected and the Attach button highlighted

By default in VS2008 Express, only a select number of exceptions are caught by default by the debugger. However, you want to make sure all exceptions are caught, so make sure that the checkmark next to "Win32 Exceptions" is fully black-and-white and not gray.

A debugger Exceptions window with the term Break when an exception is thrown solidly checkmarked for all Win32 exceptions

In the Java application, click on the Test Bad Function button.

When the badFunction is called, and it attempts to write to an invalid memory location, the Visual Studio debugger will catch this exception and show you the line of code responsible.

When an exception is thrown while debugging the JNI DLL, Visual studio shows this window with Break, Continue, Ignore as options; the Break button is highlighted so you can stop exactly at the code that failed

This should provide you the information needed to implement a fix.

Archive Contents

    Length      Date    Time    Name
  ---------  ---------- -----   ----
          0  2010-07-07 17:24   test-jni-c/
          0  2010-07-07 17:24   test-jni-c/test-jni-c/
        901  2010-07-07 15:07   test-jni-c/test-jni-c.sln
        410  2010-07-07 15:07   test-jni-c/test-jni-c/dllmain.cpp
        311  2010-07-07 17:11   test-jni-c/test-jni-c/org_vilimpoc_Jni.cpp
        613  2010-07-07 16:57   test-jni-c/test-jni-c/org_vilimpoc_Jni.h
       1305  2010-07-07 15:07   test-jni-c/test-jni-c/ReadMe.txt
        297  2010-07-07 15:07   test-jni-c/test-jni-c/stdafx.cpp
        468  2010-07-07 17:04   test-jni-c/test-jni-c/stdafx.h
       1428  2010-07-07 15:07   test-jni-c/test-jni-c/targetver.h
        696  2010-07-07 17:04   test-jni-c/test-jni-c/test-jni-c.cpp
        851  2010-07-07 15:07   test-jni-c/test-jni-c/test-jni-c.h
       5390  2010-07-07 17:03   test-jni-c/test-jni-c/test-jni-c.vcproj
          0  2010-07-07 17:24   test-jni-java/
          0  2010-07-07 17:25   test-jni-java/build/
       3734  2010-07-07 15:14   test-jni-java/build.xml
         63  2010-07-07 16:28   test-jni-java/build/built-jar.properties
          0  2010-07-07 17:25   test-jni-java/build/classes/
          0  2010-07-07 16:32   test-jni-java/build/classes/.netbeans_automatic_build
          0  2010-07-07 17:25   test-jni-java/build/classes/META-INF/
          0  2010-07-07 17:25   test-jni-java/build/classes/META-INF/services/
         23  2010-07-07 16:28   test-jni-java/build/classes/META-INF/services/org.jdesktop.application.Application
          0  2010-07-07 17:25   test-jni-java/build/classes/org/
          0  2010-07-07 17:25   test-jni-java/build/classes/org/vilimpoc/
        347  2010-07-07 16:56   test-jni-java/build/classes/org/vilimpoc/Jni.class
          0  2010-07-07 17:25   test-jni-java/build/classes/org/vilimpoc/resources/
       8187  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/about.png
          0  2010-07-07 17:25   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/
       3588  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon0.png
       3585  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon1.png
       3568  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon10.png
       3581  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon11.png
       3589  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon12.png
       3586  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon13.png
       3586  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon14.png
       3585  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon2.png
       3572  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon3.png
       3576  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon4.png
       3580  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon5.png
       3581  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon6.png
       3598  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon7.png
       3594  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon8.png
       3581  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/busy-icon9.png
       3360  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/busyicons/idle-icon.png
      21747  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/splash.png
        278  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/TestJniAboutBox.properties
        433  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/TestJniApp.properties
       1221  2010-07-07 16:28   test-jni-java/build/classes/org/vilimpoc/resources/TestJniView.properties
       6059  2010-07-07 16:32   test-jni-java/build/classes/org/vilimpoc/TestJniAboutBox.class
       1394  2010-07-07 17:08   test-jni-java/build/classes/org/vilimpoc/TestJniApp.class
        885  2010-07-07 17:18   test-jni-java/build/classes/org/vilimpoc/TestJniView$1.class
       1109  2010-07-07 17:18   test-jni-java/build/classes/org/vilimpoc/TestJniView$2.class
       2281  2010-07-07 17:18   test-jni-java/build/classes/org/vilimpoc/TestJniView$3.class
        751  2010-07-07 17:18   test-jni-java/build/classes/org/vilimpoc/TestJniView$4.class
        751  2010-07-07 17:18   test-jni-java/build/classes/org/vilimpoc/TestJniView$5.class
        721  2010-07-07 17:18   test-jni-java/build/classes/org/vilimpoc/TestJniView$6.class
      10389  2010-07-07 17:18   test-jni-java/build/classes/org/vilimpoc/TestJniView.class
          0  2010-07-07 17:25   test-jni-java/build/empty/
          0  2010-07-07 17:24   test-jni-java/dist/
          0  2010-07-07 17:24   test-jni-java/dist/lib/
     264192  2010-07-07 16:28   test-jni-java/dist/lib/appframework-1.0.3.jar
      11007  2010-07-07 16:28   test-jni-java/dist/lib/swing-worker-1.1.jar
       1452  2010-07-07 16:28   test-jni-java/dist/README.TXT
      28160  2010-07-07 17:11   test-jni-java/dist/test-jni-c.dll
     846848  2010-07-07 17:11   test-jni-java/dist/test-jni-c.pdb
     118420  2010-07-07 16:28   test-jni-java/dist/test-jni-java.jar
         82  2010-07-07 15:14   test-jni-java/manifest.mf
          0  2010-07-07 17:24   test-jni-java/nbproject/
      48407  2010-07-07 15:14   test-jni-java/nbproject/build-impl.xml
        475  2010-07-07 15:14   test-jni-java/nbproject/genfiles.properties
          0  2010-07-07 17:24   test-jni-java/nbproject/private/
          0  2010-07-07 16:50   test-jni-java/nbproject/private/config.properties
        230  2010-07-07 16:50   test-jni-java/nbproject/private/private.properties
        211  2010-07-07 17:24   test-jni-java/nbproject/private/private.xml
       2358  2010-07-07 16:50   test-jni-java/nbproject/project.properties
        721  2010-07-07 15:14   test-jni-java/nbproject/project.xml
          0  2010-07-07 17:24   test-jni-java/src/
          0  2010-07-07 17:24   test-jni-java/src/META-INF/
          0  2010-07-07 17:24   test-jni-java/src/META-INF/services/
         23  2010-07-07 15:14   test-jni-java/src/META-INF/services/org.jdesktop.application.Application
          0  2010-07-07 17:24   test-jni-java/src/org/
          0  2010-07-07 17:24   test-jni-java/src/org/vilimpoc/
        207  2010-07-07 16:56   test-jni-java/src/org/vilimpoc/Jni.java
          0  2010-07-07 17:24   test-jni-java/src/org/vilimpoc/resources/
       8187  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/about.png
          0  2010-07-07 17:24   test-jni-java/src/org/vilimpoc/resources/busyicons/
       3588  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon0.png
       3585  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon1.png
       3568  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon10.png
       3581  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon11.png
       3589  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon12.png
       3586  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon13.png
       3586  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon14.png
       3585  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon2.png
       3572  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon3.png
       3576  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon4.png
       3580  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon5.png
       3581  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon6.png
       3598  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon7.png
       3594  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon8.png
       3581  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/busy-icon9.png
       3360  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/busyicons/idle-icon.png
      21747  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/splash.png
        278  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/TestJniAboutBox.properties
        433  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/resources/TestJniApp.properties
       1221  2010-07-07 15:33   test-jni-java/src/org/vilimpoc/resources/TestJniView.properties
      11611  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/TestJniAboutBox.form
       7429  2010-07-07 15:14   test-jni-java/src/org/vilimpoc/TestJniAboutBox.java
       1223  2010-07-07 16:54   test-jni-java/src/org/vilimpoc/TestJniApp.java
      11613  2010-07-07 17:18   test-jni-java/src/org/vilimpoc/TestJniView.form
      11839  2010-07-07 17:18   test-jni-java/src/org/vilimpoc/TestJniView.java
          0  2010-07-07 17:24   test-jni-java/test/
  ---------                     -------
    1585637                     112 files
Last updated ➡️ 2 April 2023 (mobile friendly + image alt text + zip file contents)