Search code examples
javawindowsdlljava-native-interfacenative

Exiting from Native code in JNI kills Java application


I am calling a method of my native dll from JNI. It works fine but at one place in a my native code, I am calling exit(1) so that native code stops working but what happens is that it also causes to exit from my java application. Am I missing something? Is there any method in JNI so that I can only kill native code instead of whole java application?

Edit: Basically I am encrypting a file in native code. I want it to stop encrypting when some one cancels the operation from java.

Any help would appreciated.


Solution

  • JNI native code and the JVM run in the same process. Since exit shuts down the process, it will also end the JVM.

    Killing a thread which executes in native code is potentially leaking resources and therefore not supported. Look here for more details.