0

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?

1 Answers1

-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