The Source for Java Technology Collaboration
User: Password:



Kohsuke Kawaguchi

Kohsuke Kawaguchi's Blog

JNI Template Library

Posted by kohsuke on February 08, 2006 at 10:33 PM | Comments (4)

I posted the first version of JNITL, a template library for developing C++ JNI application.

As a C++ programmer, I always felt disappointed to see that JNI only supports C binding. Well, to be fair, it does a bare minimal C++ handling, like making JNI functions methods of the JNIEnv class, but any C++ programmer knows that we can do a lot better than that.

I've been writing a few native programs lately that interface with Java (like com4j, NLink, and kprofiler), and the lack of good C++ support was really getting painful. Just to take one example, to invoke a Java method from JNI, you have to do:

  1. Obtain a class ID by giving a name (analogous to Class.forName)
  2. Obtain a method ID by giving a signature (analogous to Class.getMethod)
  3. Then use a JNI function to invoke this method.

Naturally, I wanted to do this easily. The obvious way to do this is to have a class that wraps the ID acqusition logic, so there goes two classes --- one for a class, another for a method. And then, when you invoke a method, you'd have to call a different JNI function depending on the return type of the method.

Since I can't care less about those details, I thought I should be able to just parameterize the method wrapper like this:

jnitl::JClass aClass("org/acme/Foo");
jnitl::JMethod<jint> aMethodThatReturnsInt(aClass,"methodName","ILjava/lang/Object;");

jint r = aMethodThatReturnsInt(object,arg1,arg2,...);

In doing this, I realized that this "call different function based on type" notion is very commonly seen in many places of JNI. So it makes sense to define another template so that mapping from type to those JNI methods can reused in other places.

In this way, in no time I found myself writing some 20 or so templates and their specializations to make those things work in a way it's supposed to be. I don't know how many of you out there still plays with C++, but for those of you who do, I hope you find this useful (or not, in which case I'd love to learn how it can be improved.)


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • hi kohsuke,

    thanks for the library.

    pls. keep adding stuff. I'm planning on writing a C++ GUI which would need to use JNI to use JMS. This will help.

    BR,
    ~A

    Posted by: anjanb2 on February 09, 2006 at 06:36 PM

  • Kohsuke,

    Please take a look at http://sourceforge.net/projects/jace/

    It is quite mature and powerful. I am a co-developer and would love to have some help maintaining the code. Please contact me so we can discuss this.

    Thanks,
    Gili

    Posted by: cowwoc on February 12, 2006 at 10:18 AM

  • Hi kohsuke,
    jnitl looks quite useful, though I currently don't have a C++ IDE installed, so I can't try it out ;-(
    Just a few remarks:
    Many (if not all) jnitl classes are non-copyable because of their RAII nature, so they should declare private copy-ctors and assignment operators.
    JString allocates memory using array-new, but deletes it using scalar-delete. That's undefined behvaiour.
    JString uses memcpy to copy an array of jchar (defined to be 16-bit-entities) into an array of wchar_t (with an implementation-dependent size). That may work on Windows, but may fail elsewhere. Better use a for-loop to copy the array.


    Posted by: mcnepp on February 16, 2006 at 12:25 PM

  • See what I have done for MS Visual Studio at
    http://www.simtel.net/product.php[id]94368[sekid]0[SiteID]simtel.net
    http://www.simtel.net/product.php[id]93174[sekid]0[SiteID]simtel.net

    See also the article at
    http://codeproject.com/cpp/OOJNIUse.asp

    It is not enough to have some templates for JNI coding. The main problems are java references and synchronization of multithreaded JNI calls with java code.
    In my tools I have solved there problems, they create code while a project design. Developer does not bother about java references, how to find proper JVM and load it. My tools can generate implementation of native methods in object-oriented manner, can implement java interface in JNI without java coding, etc.
    Now I am finishing .NET version of Low-level JNI API. It includes classes that create java multidimensional arrays of any type. But for implementing java native methods should be written unmanaged code as .NET managed code does not support export functions.

    Posted by: vitallis on April 16, 2006 at 09:16 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds