How to call c++ functionality from java

c++, jar, java, java-native-interface, system-calls

Solution

Assuming no better communication method is available (SOAP, ICE, Sockets, etc), I'd call the executable using `Runtime.exec()`. JNI can be used to interface directly, but I wouldn't recommended it. No you can't put an executable in the jar. Well you can, but you can't run it, since the shell doesn't know how to run it.

Problem

I have a Java program that is mostly GUI and it shows data that is written to an xml file from a c++ command line tool. Now I want to add a button to the java program to refresh the data. This means that my program has to call the c++ functionality. Is the best way to just call the program from java through a system call? The c++ program will be compiled for mac os and windows and should always be in the same directory as the java program. I would like to generate an executable can the c program be stored inside the jar and called from my program?

Original source