I'm calling a method from an external library that includes System.exit(), shutting down the JVM when the method is called. Is there a way to prevent the JVM created through JRuby to run System.exit() if encountered?
Asked
Active
Viewed 89 times
0
-
I wouldn't expect anything good to come from ignoring `System.exit` – Elliott Frisch Jul 06 '18 at 23:02
-
2Possible duplicate of [Java: How to test methods that call System.exit()?](https://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit) – Jared Stewart Jul 06 '18 at 23:18
-
The only real solution is to complain to the author(s) of that library that they shouldn't be using `System.exit(...)` – Mark Rotteveel Jul 07 '18 at 12:48
1 Answers
-1
you could install a SecurityManager for the JVM, using JRuby :
class SecurityManagerImpl < java.lang.SecurityManager
def checkExit(status)
raise java.lang.SecurityException.new("HALTED System.exit(#{status})") if status == 42
end
# optionally - disable all other checks from happening :
superclass.instance_methods(false).select { |name| name.to_s.start_with?('check') }.each do |name|
class_eval "def #{name}(*args) end" if name != :checkExit
end
end
java.lang.System.setSecurityManager(SecurityManagerImpl.new)
now, all exit(42)
calls will fail with a Java::JavaLang::SecurityException (HALTED System.exit(42))
which need to be handled up the call stack.
be aware that this is likely a very "hacky" work-around to have in place ...

kares
- 7,076
- 1
- 28
- 38