views:

610

answers:

2

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.

+3  A: 

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.

Francis
Yes I could, but I'd very much prefer calling the whole stuff directrly from java. . . It would enavble me to experiment faster. The library I use is a mess that hundreds of functions, so writing a full wrapper is not an option, and doing it bit by bit will be a pain too.
jb
I'm not suggesting the JNI, so there's no need to write complex things in wrapper. In fact it's easy - prefix _declspec(dllexport) to every function in the header (which can be simply done by editor's search-replace) and build as a DLL project, then it should give you a DLL.
Francis
I'll try it, thanks.
jb
+2  A: 

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 :-)

Kevin Day