开发者

Exporting Images with JAR in Eclipse (Java)

开发者 https://www.devze.com 2023-02-08 13:59 出处:网络
I\'ve been working on a little project that requires external images for display. I\'m not all that familiar with how to use Eclipse and this is my first time attempting to export a completed project

I've been working on a little project that requires external images for display. I'm not all that familiar with how to use Eclipse and this is my first time attempting to export a completed project so I can share it with ot开发者_StackOverflow中文版hers. Right now, it seems the only way I can get my images to show up is if I assign a specific folder on my hard drive and have the image paths in the code go to that. I'm looking for a way to export the images as part of my JAR or as part of the same package so when I go to send this program to other people, I don't have to send them a separate archived folder of images. I'd also be interested in learning what I need to do to have my code reference the images within that package so they'll work without an external folder. I have read about some kind of package system within Eclipse, but have thus far had no luck in figuring out how to use it. Could use some explicating!

Thanks in advance to anyone willing to give me their two cents.


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

Exporting Images with JAR in Eclipse (Java)


Just drag the images folder into your Eclipse project, then choose to "Copy New Folder" or "Copy File and Folder" depending on Eclipse version, and then right click on the image folder (in Eclipse) and --> build path "use as source folder".


you might need to load them as class path resources if they are within a jar. see: getClass().getClassLoader().getResourceAsStream(...)


Use getResource() to load the images:

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


If you're using JDK 1.7 or JDK 1.8, you might want to use the NIO.2 API.

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 a java.nio.file.FileSystem, you can call the method to get the FileSystem for the JAR file.

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

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

If you would like to be able to run this in Eclipse, make sure you surround the call to the method with a try/catch IOException and assign to your FileSystem object the following.

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

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


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


If you add a folder to build path you can retrieve the images either in eclipse and when you exported it in jar file, just remember to don't reference the image with the path like img/myImage.gif but only myImage.gif !

0

精彩评论

暂无评论...
验证码 换一张
取 消