-3

I have a c++ application which in essence makes a connection to a server blocks on it to listen and when it gets a message from the server, dispatches the message received to a thread and then blocks on listen again (forever).

The question here is can I use JNI to invoke the c++ application (call the main function of the c++ code) and get the message back to the java layer and process it? The literature says JNI is used for invoking c/c++ libraries from java but can the above c++ application be considered a library? Or is JNI just used for calling c++ native functions where in a call from java is made and the control passes to c++ and then back to java?

The c++ application above use pthreads and blocks on listen and the control would pass only to java layer when a message which was processed from c++ layer is passed to java layer. Any pointer for clarification on the above would be much appreciated.

ugadi
  • 9
  • 1
    If you control the source to the C++ application, you could build a library from the same source code for use from a Java application with JNI. However, it might be simpler to launch it as a child process from your Java application, and read the messages it receives from its stdout. – Andy Thomas Jan 05 '22 at 18:00
  • You may need to read the following SO posts for better understanding: - [Is it possible to implement a Java Interface in C or C++ using JNI?](https://stackoverflow.com/questions/18903964/is-it-possible-to-implement-a-java-interface-in-c-or-c-using-jni) and [Java Interface in C++](https://stackoverflow.com/questions/25851296/java-interface-in-c/25851449#25851449) And since you have mentioned you have C++ application, may be following can also help: - [How do you declare an interface in C++?](https://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c) – BZKN Jan 05 '22 at 19:32
  • 1
    You can consider an application’s `main` function to be a function like any library function, but when you build the application as an executable, you can not *load* it like a library on most systems. The literature you’ve used was imprecise, at best. JNI can only call dedicated functions, handling the invocation from Java. To call other native functions, you need a stub function handling the JNI invocation conventions and delegating to the actual function. The upcoming [Foreign API](https://docs.oracle.com/en/java/javase/17/docs/api/jdk.incubator.foreign/module-summary.html) may change this. – Holger Jan 07 '22 at 13:24

1 Answers1

-1

The literature says JNI is used for invoking c/c++ libraries from java but can the above c++ application be considered a library?

Yes, it can.

Or is JNI just used for calling c++ native functions where in a call from java is made and the control passes to c++ and then back to java?

It isn't clear what distinction is being made, but yes JNI can be used to call native functions written in C++ and after that call control would return back to Java.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47