Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Use native methods to expand the Java environment

Learn how to interact with libraries and applications written in other languages

  • Print
  • Feedback

Page 2 of 7

  • You want to implement a small portion of time-critical code in a lower-level language such as Assembly.

    You have written a Monte Carlo application that simulates your portfolio, but it runs too slowly. You've tried the JIT (just-in-time) compiler, but it just doesn't do it for you.



  • What does JNI allow me to do?

    • Create, inspect, and update Java objects (including arrays and strings).

      If you are using the Invocation API for embedding you can take a two-dimensional array of strings and pass it to the Java program. If your C program is called from Java, you can create and return an array of floats or an array of strings that will be understood by the Java calling method.

    • Call Java methods.

      The ability to call a Java method from C/C++ or Assembler is essential to the Invocation API. These JNI functions allow us to call static and non-static Java methods with arguments.

    • Catch and throw exceptions.

      Good programmers raise and catch exceptions. Using C/C++ or Assembler native code to raise exceptions makes for more understandable programs.

    • Load classes and obtain class information.

      A foreign-language program can request information about the class being loaded.

    • Perform runtime type checking.

      The parameters are checked at runtime, not statically as they were in the previous implementation.



    Putting the JNI to use once you know when and why to use it is no small task. Proper use requires that you understand numerous technical details. My advice is to pour over as many examples as possible and read the specification in its entirety. (In fact, it may be helpful to view the pdf version of the spec in another window while you work through this article.) When you feel comfortable with the JNI fundamentals, the rest of this article should fall into place.

    Calling Java code from C programs
    Note: Depending on which language you are using to call Java, the terminology you use may differ from what I use in this section. Please substitute the appropriate differences (for example, function vs. method vs. subroutine) for the language you're using. Because I am unwilling to rewrite useful pieces of Java code in C or C++ for my C programs, I often call Java directly from my C programs. Building a C/C++ or Assembler application that calls a Java method is much less involved than writing a C program to be called from a Java application. As you'll see later in the article, the latter technique requires us to make any modifications to the Java application. In this case, however, we drive the Java application from C, so to speak.

    For the C program to communicate with the Java program, we need to use several JNI method calls (more detail on these in a moment). Let's take a look at the steps required to interface to an existing Java application:

    1. Create the Java VM.

      To create the JVM you call JNI_CreateJavaVM(), which returns a pointer to the JNI interface pointer. The calling thread is considered the parent thread. Once you have created a VM, you can call Java methods using the standard calling procedure of getting an object, finding its class, getting the method id, and then calling the method id with the appropriate method.

    • Print
    • Feedback

    Resources