PerilousApricot
PerilousApricot

Reputation: 643

How to trap System.exit() in code called from JNI

I'm writing a C interface to a java library that calls System.exit(). I call:

/* Calls the main method for the class */
printf("about to call main\n");
(*env)->CallStaticVoidMethod(env, mainClass, mainMethod, args);
printf("returning from main\n");

I (unfortunately) don't have the option of changing the library, but I'd still like for the JVM to return control back to the C calling function (so I can do various cleanup tasks, etc..). Is there a way to get JNI to do that, or am I SOL?

Thanks,

Upvotes: 3

Views: 1924

Answers (1)

bestsss
bestsss

Reputation: 12066

You do not need bytecode editing for so simple case, a lot of security handling is implemented in the good old java.

System.setSecurityManager(SecurityManager) throw some Error (like ThreadDeath) in checkExit() and assuming System.exit(int) [erm Runtime.getRuntime().exit(int)] is invoked in the same thread, it should do it.

Upvotes: 8

Related Questions