Export JAR images to Eclipse (Java)

I was working on a small project that requires external images to display. Not everyone is familiar with how to use Eclipse, and this is my first attempt to export a completed project so that I can share it with others. Right now, it seems the only way to get my images to show is if I assign a specific folder to my hard drive and I have image paths in the code. I’m looking for a way to export images as part of my JAR or as part of the same package, so when I send this program to other people, I don’t need to send them a separate archived image folder. I would also be interested to know what I need to do so that my code refers to images inside this package so that they work without an external folder. I read about some kind of package system in Eclipse, but still no luck in how to use it. Some explanations can be used!

Thanks in advance to anyone who wants to give me two cents.

+2
source share
6 answers

you may need to load them as class path resources if they are in the bank. see getClass (). getClassLoader (). getResourceAsStream (...)

+2
source

Something I found useful with this answer is this: make sure you put your images / files in the same eclipse folder (or a subfolder below) as your source code. I created the "images_ignored" folder using eclipse, adding it to the build path, but still refused to be included in my JAR file (when creating an executable JAR).

Eclipse screenshot showing were to put images

+10
source

Just drag the image folder into the Eclipse project, then select “Copy New Folder” or “Copy File and Folder” depending on the version of Eclipse, and then right-click on the folder with images (in Eclipse) and use → build path as the source folder ".

+6
source

Use the getResource () function to load images:

ImageIcon qmarkIcon = new ImageIcon(getClass().getResource("images/mark.gif")); 
+1
source

If you are using JDK 1.7 or JDK 1.8, you can use the NIO API. 2.

 for (FileSystemProvider provider : FileSystemProvider.installedProviders()) { if ("jar".equals(provider.getScheme())) return provider.newFileSystem((new File(Start.class .getProtectionDomain().getCodeSource().getLocation().toURI())) .toPath(), new HashMap<String, Object>()); } 

If you enter this code into a method that returns java.nio.file.FileSystem, you can call the method to get the file system for the JAR file.

To access the files inside your JAR file, you can use the following method, which then allows you to read the files, but you may want to.

 fileSystem.getPath("/images/image.gif") 

If you want to be able to run this in Eclipse, make sure you surround the method call with an IOException try / catch and assign your FileSystem object as follows.

 new File(Start.class.getProtectionDomain().getCodeSource().getLocation().toURI()) .toPath().toString(); 

This will allow you to run your program, whether it is compressed into a JAR file or not.


I recommend that you get used to using NIO.2, as it is a very powerful API.

0
source

If you add a folder to create the path, you can get the images either in the eclipse or when exporting to the jar file, just remember not the link to the image using the path, for example img/myImage.gif but only myImage.gif !

-1
source

All Articles