0

I have a java application. And It loads images from a directory

      "C:\\Users\\elliotbewey\\Desktop\\IMGS\\BOB.png"

And it loads fine the image displays. Now If I was to compile it into a runnable jar and send it to my friend. it would not work. What would I need to do get a installer? And get system information? Help!

//note I do understand that its going to my friends computer so \\users\\elliotbewey would not work because its his computer thats what I need help with
  • What is a purpose of those images? (Yes! You've read it correct) – Branislav Lazic Aug 14 '14 at 17:36
  • How do you load the images - please show your code? – home Aug 14 '14 at 17:36
  • In my app I load images I create onto a JPanel but that via Graphics but that isn't necessary to ask. –  Aug 14 '14 at 17:37
  • The images load fine but read the question –  Aug 14 '14 at 17:37
  • Sorry worried it wrong 1 second –  Aug 14 '14 at 17:37
  • I've read it. Now you read my question. – Branislav Lazic Aug 14 '14 at 17:37
  • Okay reload page its fixed sorry about that I rushed. –  Aug 14 '14 at 17:38
  • @elliot5: this *is* important as you need the images in your classpath, so that you can distribute with your code. – home Aug 14 '14 at 17:38
  • If your images are not supposed to be changed. I.e. You will always load same image to JPanel, then add them to some package and load as a resources. – Branislav Lazic Aug 14 '14 at 17:39
  • Sorry before I worded it wrong reload. I think I know the answer I need a installer and to get System propity PC name then put that in to the path. –  Aug 14 '14 at 17:40
  • No you don't! I see what you want to do. You want to read users name and to "build" path to image. New issue will appear if you try to run it on some different OS. Such as OSX. – Branislav Lazic Aug 14 '14 at 17:41
  • Oh yeah because it would not be C: good spot –  Aug 14 '14 at 17:41
  • Well all the path really so how Would I do it for all platforms ect? –  Aug 14 '14 at 17:42
  • Have a look at this [question](http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java) – Branislav Lazic Aug 14 '14 at 17:45
  • Lets say If it was a PC only game then. And I used http://www.ej-technologies.com/products/install4j/overview.html and I got the PC name and set it in the build path. After installing it via the installer would that work for PC? –  Aug 14 '14 at 17:45

2 Answers2

0

You need to package your images inside your jar, then reference them as resources. Doing a google search for "java package images inside jar as resource" will give you a ton of results and tutorials.

This approach will work from an IDE on your computer or from a packaged jar on your friend's computer. Using an installer is definitely overkill for this goal.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Thanks! But the goal will be large. I was giving a example. –  Aug 14 '14 at 17:56
  • I'm not sure what you mean by that, but either way, you should include these files inside your jar and reference them as resources. – Kevin Workman Aug 14 '14 at 18:00
  • "Using an installer is definitely overkill for this goal" –  Aug 14 '14 at 22:07
  • Even so. If your goal is to include images with your project, no matter how many images you have, including them inside the jar and referencing them as a resource is the way to go. The only reason to use an installer is if you want something natively executable or to include the JRE along with your program. You'd still package your images into the jar, even if you were packing the jar up as a native executable. – Kevin Workman Aug 14 '14 at 23:15
  • My answer is still correct. Sounds, videos, images, it really doesn't matter. That's all content that belongs in the jar. The only exception is if you want users to be able to modify that content. The point is that there's no reason to create a packaged executable just to include resources with your jar. – Kevin Workman Aug 19 '14 at 00:06
  • I never said it was not correct I was saying I now am using sounds in my jar aswell as images. And saying help for the thanks –  Aug 19 '14 at 00:11
  • Down the rabbit hole: I never said you said it wasn't correct. I'm just making sure you understand my answer. Glad you got it working. – Kevin Workman Aug 19 '14 at 12:35
0

Locate your images inside jar file (for example res/image.png). Then simply call this to get BufferedImage:

BufferedImage image = ImageIO.read(YourClassName.getClass().getResourceAsStream("/res/image.png"));
Arvy
  • 95
  • 12