tags:

views:

490

answers:

3

I have two options - I can either use JNI or use Runtime.exec to execute a C++ library.

The C++ program is CPU intensive and long running i.e. some calls may take up to a couple of hours to return.

What are the pros and cons of each? Which approach should I go for?

+4  A: 

If you need to interact with the C++ library, go for JNI.

If it's a standalone program that you just want to invoke (and perhaps grab the output), Runtime.exec() is much, much simpler.

Michael Myers
+1  A: 

Have you looked into JNA as a third option?

From the JNA site:

JNA provides Java programs easy access to native shared libraries (DLLs on Windows) without writing anything but Java code—no JNI or native code is required. This functionality is comparable to Windows' Platform/Invoke and Python's ctypes. Access is dynamic at runtime without code generation.

See their getting started guide for an introduction.

Steve K
thanks, hadn't heard of JNA. +1
dogbane
A: 

Using JNI may restrict your ability to move from 32bit to 64 bit. You may also find you have to tune the application memory settings as well.

Unless you know how well the C++ stuff is written - it could make your app more unstable. You are lucky in that the C++ bit takes a few hours - so just call it externally.

Fortyrunner