Search code examples
javashelljava-native-interfaceruntimejna

JNI/JNA vs Runtime.getRuntime().exec(String)?


To my knowledge, anything that I can do in a command shell, I can do from JNI, JNA or Runtime.getRuntime().exec(String). So which to use when?

Is it that JNI/JNA allow me to communicate (interprocess communication, etc.) with a running native application that otherwise couldn't be performed from a command shell? Or is it just that Runtime.getRuntime().exec(String) is platform-independent, whereas JNI/JNA require you to know the platform your on in order to use them?


Solution

  • In a nutshell,

    • JNI and JNA permit using native libraries in the same process.
    • Runtime.exec (and it's newer friend ProcessBuilder) launch new external applications.

    Since each requires cooperation with the host operating system and an existing native binary (whether it's a library or a runnable program) none of them is platform-indepedent.

    None of the three permits communicating with a running external application. To do that you need an inter-process communication mechanism, which can be implemented on top of shared memory, files, pipes, or sockets for example.