Get Jruby jar path
Like this question , I would like to know how to get the current path to the jar file. But unlike the other question, I want to know the jar path, not the path to the current file inside the jar. For example, let's say I had a jruby jar in "/Users/MyUser/Jars/foo.jar" and inside there was a compiled ruby file in "foo.jar / java / style / class / path.class". How can class.path get the path "/Users/MyUser/Jars/foo.jar"?
I've also seen a way to do this in Java , but I just don't know how to translate it ...
source to share
I've experimented a bit with this and any attempt to call the code from JRuby returns the jruby jar itself, which makes sense since the logic is actually being evaluated / executed from that jar. For example:
require 'java'
puts self.to_java.get_class().protection_domain().code_source().location().path()
or
require 'java'
class Path
def get_jar_path
self.to_java.get_class().protection_domain().code_source().location().path()
end
end
puts Path.new.get_jar_path
both return a path to jruby-complete.jar
, which may be useful to some extent, but clearly not what you are looking for.
The only way to get this to work (with rawr
, but it's easy to adapt if you use warbler
) is to create a Java class in src/java/org/rubyforge/rawr
:
package org.rubyforge.rawr;
public class Path {
public String getJarPath() {
return getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
}
}
Then call this class from your JRuby script:
require 'java'
java_import 'org.rubyforge.rawr.Path'
puts Path.new.get_jar_path
source to share