Need to call a C++ method which takes user defined classes as arguments (for ex: calculateSalary(Employee[] empVector)).

Alexander Krapf

First, you should get a really good book about JNI if you're going to use anything but the most trivial methods. I recommend Rob Gordon's or Sheng Liang's books on Java (just search for the authors at your favorite online bookstore).

Every non-primitive instance is passed as a jobject. Arrays are objects in Java, so they are passed as jobject instances too. JNI has functions for accessing elements of arrays and other functions for getting data out of instances of any type or calling methods on instances of any type. It gets pretty involved at that level and it is easy to make mistakes that are non-trivial to debug. This is one of the reasons JNI has such a bad name.

Once you know your way around, you can write a utility API that helps you in dealing with the problems or you can purchase a commercial solution. I happen to be working for Codemesh Inc. and we have a tool called JunC++ion. This tool will generate C++ proxy classes for Java classes. These proxy classes can be used in the implementation of native methods, making your life much easier when you're talking about complex API's.

Let me give an example. Using JunC++ion, your usecase might look like:

  _cmj_Java_..._calculateSalary( ..., Employee::array1D & empVector )
    if( empVector == null || empVector.length == 0 )
      throw IllegalArgumentEcxeption( "empVecor" );

    for( int i=0; i<empVector.length; i++ )
      Employee e = empVector[ i ];


If you compare this to a corresponding JNI implementation, you will see the benefit of our approach.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.