Home arrow Java arrow Page 2 - Interfacing With C From Java With JNI: Part 2/2
JAVA

Interfacing With C From Java With JNI: Part 2/2


In the final article of the series, Neville talks about advanced JNI and shows us a great example that takes us 1 step closer to mastering the basics of JNI.

Author Info:
By: Neville Mehta
Rating: 4 stars4 stars4 stars4 stars4 stars / 39
July 14, 2002
TABLE OF CONTENTS:
  1. · Interfacing With C From Java With JNI: Part 2/2
  2. · Advanced JNI
  3. · Advanced JNI (contd.)
  4. · Conclusion

print this article
SEARCH DEVARTICLES

Interfacing With C From Java With JNI: Part 2/2 - Advanced JNI
(Page 2 of 4 )

In the last article I discussed a specific code snippet with you that would call a simple display method in C, which in turn would display a "Hello World" message on the screen. You will come across numerous scenarios where you will need to pass objects to the C method and probably the C method will return an object too. In this part of the article we will discuss comparatively more advanced coding and concepts.

When passing or returning values in the form of parameters from Java to C or vice versa, we have to follow a mapping system. Let's assume you want to pass an integer parameter from Java to C. Firstly, you just check the mappings (which are in form of a table) and note the corresponding mapping for the integer data type. Next, you pass your parameter from you Java Code as a normal integer but collect it from your C code as the data type which corresponds to the integer parameter in the mappings.

Here's the table of JNI Java To C/C++ mappings:

The Java to C/C++ mappings

It works like this: when you pass a long variable parameter to a C method for example, you collect it from the C Method in the form of a jlong variable. Once we know the Java to C/C++ mappings, it's not so hard to figure out the C/C++ to Java Mappings. Yes, just reverse the table.

When you want to return say a String variable to you Java Code, you will actually return a jstring variable in your C code. Let me ask you another question: are you confused? Well, I think some of you will be to an extent, but trust me, when you see the code you will wonder what you were confused about.

Let's start by coding our Java file from which we will call the native method. Save this code as NevNative.java in your favorite text editor:

public class NevNative {

static private int array[] = {'1','2','3'};
static private byte bit = 'b';
static private long lon = 999;

public native String getParameters(byte inByte,long inLong,int [] inArray);

static {
System.loadLibrary("nevnative");
}

public static void main(String[] args) {
NevNative object=new NevNative();
String retval = object.getParameters(bit,lon,array);
System.out.println(retval);
}
}


We are declaring a native method called getParameters, which will be implemented in C (that's the reason why we leave it empty here). The getParameters method takes a byte, long and array as parameters and returns back a string. In the main method we just make an object of our class and call that native method.

The rest of the code should be fairly self explanatory after reading part 1 of this article.

static {
System.loadLibrary("nevnative");
}


Before we proceed, let's compile the Java file that we created:

C:\jdk\bin\javac workingdir\NevNative.java

After we successfully compile our Java file we can now create our header file. We use the javah compiler to do so:

C:\jdk\bin\javah workingdir\NevNative

Check your working directory and you will find a file called NevNative.h has been created. Now look for our native method declaration there. It looks something like this:

JNIEXPORT jstring JNICALL Java_NevNative_getParameters(JNIEnv *, jobject, jbyte, jlong, jintArray);

This is the method declaration for the method we will have to implement in our C File. When we refer to our mapping table, we can see in our C/C++ mappings that the String variable requires the jstring data type, the byte variable requires the jbyte data type, the long variable requires the jlong data type and finally all of the arrays require a jarray data type (jintarray, because the array is of integer type) to be successfully received from our Java code. Thus, we confirm this by having a look at our method declaration where all the JNI Java To C/C++ mappings have been correctly enforced.

You might also be wondering what that first parameter is all about? Well, we didn't pass it did we? That's the JNI Context; it's some sort of an interface. The JNIEnv pointer points internally to a function table. As a matter of fact, the JNIEnv pointer has a number of helper functions, which can help you as a JNI programmer.
blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials