Question: ruby how to load image relative to source code in jar

Question

ruby how to load image relative to source code in jar

Answers 1
Added at 2016-11-02 06:11
Tags
Question

I have a jruby Swing application in a jar. In my source code main.rb I have an image specified as

img = ImageIcon.new("img/test.png")

which does not load. My path looks like this

lib/main.rb

lib/img/test.png

Is there a way to specify the relative path to the image?

In addition, I've found the equivalent way of doing this in Java using getResource

new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))

How would I implement this in jruby?

Answers to

ruby how to load image relative to source code in jar

nr: #1 dodano: 2016-11-02 06:11

Just like in Java - your file is not a file, it has to be addressed as a resource-inna-jar:

java.net.URL url = getClass().getResource("img/test.png");
ImageIcon image = new ImageIcon(url);

Didn't try, but translated into Ruby, I believe it's something like

url = JRuby.runtime.jruby_class_loader.get_resource("/lib/img/test.png")
image = ImageIcon.new(url)
Source Show
◀ Wstecz