I have been experimenting with JNI recently, in order to port some existing C++ libraries.
As part of my testing I created a simple 'helloworld' program. I am calling a simple native function in C++, that just prints messages. I am a bit curious about some behavior I have observed while executing the program - it seems that all the native function messages/responses gets printed after Java System.out.print
's. Is this because native calls are executed after Java calls, or shall I just ignore this behavior?
public static void main(String[] args) {
HelloWorld app = new HelloWorld();
System.out.println("say");
app.print();
System.out.println("what");
app.print();
}
The output looks like this:
say
what
hola, world !
hola, world !
The native function is as follows:
Java_HelloWorld_print(JNIEnv *env, jobject obj) {
printf("hola, world !\n");
return;
}
Is this because native calls are executed after Java calls
No, it almost certainly has to do with how the output gets buffered on the C++ and Java sides.
The execution order of the calls is exactly as it appears in your code (Java, C++, Java, C++).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With