Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI vs Runtime.exec()

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?

like image 666
dogbane Avatar asked Sep 18 '25 00:09

dogbane


2 Answers

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.

like image 107
Michael Myers Avatar answered Sep 20 '25 14:09

Michael Myers


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.

like image 20
Steve K Avatar answered Sep 20 '25 13:09

Steve K