Question

I have to use some static windows library (*.lib) from java code, I can write C++ JNI wrappers, but I'd rather not do it (not that experienced in C++).

What would be the easiest (least developement effort) way use it, performance is not important, since that code will just be fetching some data, that most probrably will be worked on on JAVA side.

Most probably I would use JNA, but AFAIK it needs dynamic libraries, and I have static ones.

I also asked question on converting static libraries to dynamic ones.

Was it helpful?

Solution

For what it's worth, I had a project like this awhile back. It was pretty easy to auto-generate the JNI wrappers. I think we had about 350 function exports to wrap. It took us about 3 hours to put together a script to auto-generate the wrapper (sorry, don't have the script laying around handy or I'd post it).

We wrote almost no C++ code ourselves - but it did require understanding how JNI works... That's actually a pretty good learning opportunity/project - if you have the time, don't be afraid of JNI - you'll be amazed at how much you learn about how the JVM works...

If you do go this route, I recommend that you keep your wrapper functions really, really lightweight - literally no processing in them at all. Just transform the necessary arguments from JNI values to native (this is mostly needed for strings), call your native function, and transform the results back.

If you have a function that passes in a string pointer and expects the string to come back in the pointer, use a string array with size 1 from the Java side and populate it with the result from the native call.

Or if you are pressed for time, compile your .lib to a .dll and use JNA :-)

OTHER TIPS

You can always create a DLL (dynamic library) project which calls directly into your static library, and then you can use JNA with the new created DLL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top