1

I have a project with a image stored as a logo that I wish to use.

URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");

Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"

However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?

Cheers

StephenHynes
  • 53
  • 1
  • 1
  • 5

2 Answers2

1

You are almost there.

Images in a jar are treated as resources. You need to refer to them using the classpath Just use getClass().getResource: something like:

getClass().getResource("/images/logo.jpg")); where "images" is a package inside the jar file, with the path as above

see the leading / in the call - this will help accessing the path correctly (using absolute instead of relative). Just make sure the path is correct

Also see:

How to includes all images in jar file using eclipse

Community
  • 1
  • 1
Vijay Agrawal
  • 3,751
  • 2
  • 23
  • 25
  • Okay, so when im referencing it to a ImageLabel (which takes in a string which is the absolute path to a file) should I remove the "jar:file:/" at the start using a substring? Im still getting the same problem, works fine in Eclipse but trips up after exporting it as a Jar file. – StephenHynes Mar 24 '12 at 17:10
  • yes, no need to use "jar:file://" - just treat it as a path to the package containing the resource. So when you package the jar, if it contains /images/logo.jpg you are good – Vijay Agrawal Mar 24 '12 at 17:20
  • When I run it as a Jar file I get the following path, "C:/Users/Stephen/workspace/ILLA.jar!/img/logo.jpg" is that normal? – StephenHynes Mar 24 '12 at 17:25
  • while that is normal, usually the URL is not consumed in this way. Where do you intend to use the URL? Typically it is passed into other APIs such as ImageIcon and such – Vijay Agrawal Mar 24 '12 at 17:28
  • I use it with a custom class that takes in a String parameter that is supposed to be a location of a file. Obviously there's some problem there with handling that path. Alternatively though I could use a ImageIcon if it is allowed to be added to a JFrame. – StephenHynes Mar 24 '12 at 17:30
  • you can do: Image img = ImageIO.read(logoPath) and use the drawImage API in the JPanel. See: http://stackoverflow.com/questions/3006162/problem-in-adding-image-to-jframe – Vijay Agrawal Mar 24 '12 at 17:36
1

See here: Create a file object from a resource path to an image in a jar file

String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));

Note it looks like you need to use a stream for a resource inside an archive.

Community
  • 1
  • 1
JasonG
  • 5,794
  • 4
  • 39
  • 67
  • See my Image file is reading in from a String not a InputStream. It's literally just looking for the path to the image. – StephenHynes Mar 24 '12 at 17:18