views:

1216

answers:

5

I'm just about to make my first trip into the world of JNI (Java Native Interface) to provide file system change notifications from platform specific C/C++ code to Java. That is unless someone suggest some brilliant library for doing this that I've missed.

Being new to JNI I've managed to find much documentation on the interface side of it and library generation side of it, but I've not found much on building the native library.

I've got an existing build based on ant for the pre-existing Java source, so I'm trying to work out if I should get ant to call make to create the library or if it's best to get make to call ant after creating the library?

Neither option jumps out as being terribly nice, but both seem better than trying to get ant to call a compiler to compile the code and generate the library directly.

+2  A: 

As a simpler alternative to JNI, try JNA: https://jna.dev.java.net/, may solve this hassle for you and be simpler (assuming it can do what you want).

Michael Neale
JNA did look like a nice way of doing what I wanted, but sadly I had to start creating new window classes and call back functions for it, which didn't seem possible so I had to fall back to using JNI.
Free Wildebeest
+2  A: 

I strongly dislike make because of its implicit ruleset and treatment of whitespace. Personally I would use cpp tasks (http://ant-contrib.sourceforge.net/cpptasks/index.html) to do my C compilation. They are not as flexible as make but they are also far less complex and it will mean you don't have to burden your developers with learning make.

stimms
CPP tasks worked great for what I needed thanks. I ended up needing to do some cross compilation but the ability to specify which class to use for the back-end for cpptasks solved that.
Free Wildebeest
+1  A: 

I'm working on something similar right now. Be aware that using swig from swig.org is often easier as it generates the stubs to the native library for you.

The short answer to your question is that the ant file should run the make file after the java library has already been built, as the native library depends on the swig generated header, which is generated from the java class files.

If you are super familiar with ant, and don't want to learn a new system, then http://ant-contrib.sourceforge.net/cpptasks/index.html, also linked by another poster, will let you build c++ in ant.

catphive
+1  A: 

I'd skip JNI entirely, and use an external program which writes notifications on standard-output. Java can then simply read from the programs output stream and generate whatever event is necessary. JNI is way too much work if all you want is to send simple notifications.

Also, on Linux you can simply start "inotifywait" (with some suitable parameters, see "man inotifywait").

JesperE
A: 

You could also try the terp C++ tasks at Codemesh. They are not free but they offer a high level of abstraction coupled with the ability to discover/specify the C++ compiler and the ability to iterate over more than one compiler/processor architecture/compiler configuration for multiplatform builds.