Reputation: 157
So I have a Java program that captures raw ethernet data in a buffer, and a C program (actually a series of C programs) that decode that data into a usable format and stick it in a file to be analyzed later. I would like to run the Java, have the data sent to the C, a file appended and control return to the original program to listen for more data.
My question is do I really need to use JNI to call this C program since I will be sending data to it, or could I just use JConfic or exec? My files aren't .exec files, so I don't really see the latter working, but I would like to keep this as simple as possible. What do you all think?
Upvotes: 1
Views: 727
Reputation: 160
you can use JNA for your purpose as you have to call C code only,but in future if you have to include calls for C++,JNA will not work,so better you use JNI which will be useful in both the cases,and performance wise also JNI has got significant advantages.
Upvotes: 0
Reputation: 63580
You have the following options:
The easiest way by far is to create a CLI executable that you can call from Java and parse its output. As an added bonus, you'll also be able to use the CLI executable from any other language.
The less easy way is to use JNA. JNA is pretty straight forward to use, but it's not supported everywhere and on every device. For example, Android does not support JNA. Check the specs for your supported devices to find out if you can use JNA with them. Also check out this tutorial for an very basic introduction to JNA.
The hard way is to use JNI. JNI is not at all straight forward to use. You will need to write lots of glue code to process JVM calls, then some other glue code to compose JVM friendly output. This is however the most widely supported method (works on Android as well). Again, do check the specs for your supported devices to find out if you can use JNI with them. If you want to take the JNI route, you can check out this tutorial for a basic introduction.
So, if you don't have very complex things to do (not many functions to support, not many data types), go with JNI. Otherwise, try a more high-level approach.
Upvotes: 2
Reputation: 86509
Yes, you can run your C programs from Java as separate processes.
Use java.lang.ProcessBuilder to make a java.lang.Process, and interact with the input, output and error streams of the Process. Note that the Process will hang if you don't read its output stream.
Upvotes: 1
Reputation: 1560
Here is a pretty straightforward example of using JNA (mentioned by @lexicore ) to call a C function from Java
http://stuf.ro/calling-c-code-from-java-using-jna/
If your Java and C program could be reorganized so that the C program called the Java program then the following article provides an excellent example with code available for download. It basically abstracts out the complexity of using JNI by using a Java Proxy class and a C facade that makes calls to the needed Java functionality.
http://java.sys-con.com/node/45840
Upvotes: 0
Reputation: 74800
Of course, you can communicate from Java with your C program using the C programs process' input and output streams.
It would depend on your problem whether this is actually easier to write than using JNI (or JNA).
Upvotes: 0