Search code examples
javajna

JNA Java make data available to other classes


I am using JNA to access a C++ Shared Libary from Java. I have one class which has inside something like that "public interface InterfaceLib extends Library" and from this I am able to call the C++ methods and it works. The thing is that I want the data generated when calling these methods available to other classes (I have Jframes and want to go from one to another). My main problem if I have understand well is that I want the data generated by this particular instance of the class calling the JNA methods. Sorry if I can't explain it well. The only solution I have found so far, which works but I don't think is the most efficient is parsing the total amount of data acquired by one run of JNA methods to some class members and then parsing these members as arguments in the constructor of the next frame etc etc. To be more specific I have one button "Run system", when I press this I run my C++ code and these generates data for THIS run, what is the most efficient way to make this data for this run available to other classes?


Solution

  • Your question isn't entirely clear to me, but I think these are the key points (the second is the more important for what you're doing):

    1. There is quite a lot of overhead when invoking native code. The data has to be translated and moved between the JVM and the host machine, and that is fairly heavy. Accordingly, you want to make sure your native calls are doing large chunks of processing. If they were, say, just adding two numbers together then it would be quicker to do it in Java and not worry about the native calls.
    2. The JNA layer does the translation for you. You will get a result back from your native call, and it's then up to you to decided what to do with it. If you want to make the data available to other classes, you can do so exactly as if the data had been generated by a Java method. The fact that it's come via a JNA call really makes no difference. Pass the resulting data to another class's method, or store it in a class field and create a getter, or write it to a file, or whatever you like.