Search code examples
jruby

Where is getProtectionDomain() in jruby?


I have a staff class in Java and I can get the location on disk where it was loaded from using the following code:

Staff.class.getProtectionDomain().getCodeSource().getLocation();

In a jruby jirb session I tried the following:

Staff.getProtectionDomain()

and (edit)

Staff.class.getProtectionDomain()

both of which cannot find the method: undefined method `getProtectionDomain'

Is this method masked by jruby and if so, how can I call it?

[edit] I am using jruby 1.5.6.


Solution

  • In jruby you should use #protection_domain() - which exists and works as expected - instead of getProtectionDomain() - which exists but does not work as expected.

    For completeness, here is a short example:

    # use this to get the jruby-complete.jar file
    a_java_class = self.to_java.java_class
      #=> class org.jruby.RubyObject
    a_java_class.protection_domain.code_source.location.path
      #=> "/C:/Users/xxx/yyy/jruby-complete-9.0.4.0.jar"
    
    
    # use this to get the file path to the commons-lang3-3.4.jar
    require_relative 'java_lib/commons-lang3-3.4.jar'
    a_java_class = Java::org.apache.commons.lang3.SystemUtils.java_class
      #=> class org.apache.commons.lang3.SystemUtils
    a_java_class.protection_domain.code_source.location.path
      #=>"/C:/Users/xxx/yyy/zzz/java_lib/commons-lang3-3.4.jar"
    

    Also, see this other post with related info: Get JRuby jar path