0

I'm trying to put images directly into my packaged JAR executable. Currently the only way to display images is having a directory with all the images in the same directory of the JAR.

Is there a way to package images directly into a JAR?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user840718
  • 1,563
  • 6
  • 29
  • 54
  • 2
    Does this answer your question? [Java Swing: Displaying images from within a Jar](https://stackoverflow.com/questions/31127/java-swing-displaying-images-from-within-a-jar) – wilddev Dec 10 '19 at 21:34
  • How are you creating the JAR? Most IDEs allow you to copy "resources" into the "source" location which will automatically be included. Maven makes use of the "resources" directory - so, the answer is, "yes", but the "how" is more difficult to answer – MadProgrammer Dec 10 '19 at 21:35
  • @wilddev That would explain how to load them at runtime, but doesn't explain how you could include them into the Jar to begin with – MadProgrammer Dec 10 '19 at 21:36
  • @MadProgrammer That depends how you build the jar. In some cases it's enough to place images together with your .java files in corresponding package directories. In gradle you can use "resources" directory and place images in corresponding package directories inside it. – wilddev Dec 10 '19 at 21:41
  • @wilddev Which is what I said - the OP isn't asking "how do I load images which are contained in the Jar", they ask "how do I put the images into the Jar" in the first place - so no, you're link won't answer their question - no offense – MadProgrammer Dec 10 '19 at 21:45
  • I use Eclipse. It's unclear to me if I have to use "resources" or "Resources". Moreover, if it must be a normal folder or a src folder on the same level of the main src folder. – user840718 Dec 10 '19 at 21:45
  • @user840718 try src folder – wilddev Dec 10 '19 at 21:48
  • @user840718 Not a user of Eclipse, but I believe you can just create a directory/folder within the `src` folder and place your images into it. Just remember, you will need to reference this folder when loading the images, ie `/path/to/images/imageName.jpg` - where `/` will be the top level package – MadProgrammer Dec 10 '19 at 21:49
  • If I create a src folder it's automatically placed on the same level of the main src folder, not inside. I can create a folder inside the main src folder, but its icon is of the package, maybe it's ok. – user840718 Dec 10 '19 at 21:52
  • 1
    There is a video how to add image resources in eclipse https://www.youtube.com/watch?v=yksgU4SxoJY – wilddev Dec 10 '19 at 21:53
  • 1
    @user840718 No, put them inside the existing `src` node - see the linked Q/A – MadProgrammer Dec 10 '19 at 21:53
  • Thank you very much! It worked! – user840718 Dec 10 '19 at 22:02

1 Answers1

0

you can put them in the resources dir. the issue then is that they don't exist on disk and you have to load them from the jar. I created a Util class to load images and do some other things.

Image image = ImageIO.read(UTILCLASS.class.getClassLoader().getResourceAsStream(path));  

where path is just the name of the file or you can give a parent dir if there are files with the same name.

mavriksc
  • 1,130
  • 1
  • 7
  • 10
  • *"you can put them in the resources dir"* would be depended on the build system. If they are using maven (or I believe intellij), then yes, but if they are using some other IDE or other build system, then this is unlikely to work – MadProgrammer Dec 10 '19 at 21:47
  • the issue is the images are in the jar and are not accessible in the way he is used to. via a path on the file system. he doesn't need help getting the files in the jar. he needs help using the bytes of the files from the jar. Thanks tho @Ma – mavriksc Dec 10 '19 at 21:49
  • *"I'm trying to put images directly into my packaged JAR executable"* is asking "how to embedded the images", not "how to load them". Yes, you need to use `getResource` or `getResourceAsStream` to load them, but the OP hasn't even get to that point yet – MadProgrammer Dec 10 '19 at 21:51