Reputation: 934
I'm trying to find a way to include a jar file in my JRuby program in runtime.
Explanation: I have the line
require 'path/to/jarSome.jar'
Now the problem is that this file doesn't exist when the program starts running, the program creates that jar file (and I'm using it later on the same program)
Is there a way to include the jar in run time? something like this:
# Some ruby code
# create/put the jar file in the location
require/import/load the jar file.???
Upvotes: 1
Views: 1841
Reputation: 934
I think I found out the answer:
class_loader = JRuby.runtime.jruby_class_loader
class_loader.add_url(java.io.File.new('path/to/jarSome.jar'))
Upvotes: 1
Reputation: 23317
You can also execute the require
statement whenever you want -- ruby is dynamic like that, you don't need to execute it on program boot or source file load.
def create_and_require_jar
do_something_to_create_jar
require 'the/jar/i/created.jar'
end
If your problem is that the jar doesn't exist yet at the moment you execute the require
-- simply change when you execute the require, to be after you've created the .jar.
While I have never created a .jar dynamically to be used by the same program that created it (and I'm not sure why you'd want to), I have done a require
of a .jar in Jruby, as part of dynamic logic where sometimes I require it and sometimes I don't, and do the require inside conditional logic, not immediately on program boot. It's worked fine.
And I've found it super nice that you can do this in Jruby, conditional loading of a Jar based on program logic (first checking to see if the jar exists, or based on arguments or configuration) -- if you can do that in straight Java, I don't know how, although you might be able to!
Upvotes: 1