简体   繁体   中英

Exporting Images with JAR in Eclipse (Java)

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 others. 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).

Eclipse屏幕截图显示的是放置图像

只需将images文件夹拖到Eclipse项目中,然后根据Eclipse版本选择“复制新文件夹”或“复制文件和文件夹”,然后右键单击图像文件夹(在Eclipse中)和 - >构建路径“使用作为源文件夹“。

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

使用getResource()加载图像:

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.

如果你添加一个文件夹来构建路径,你可以在eclipse中检索图像,当你在jar文件中导出它时,只记得不要img/myImage.gif这样的路径引用图像,但只能使用myImage.gif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM