简体   繁体   English

在Eclipse中使用JAR导出图像(Java)

[英]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. 我不熟悉如何使用Eclipse,这是我第一次尝试导出已完成的项目,以便我可以与其他人共享。 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. 我正在寻找一种方法来导出图像作为我的JAR的一部分或作为同一个包的一部分,所以当我将这个程序发送给其他人时,我不必向他们发送一个单独的存档图像文件夹。 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. 我已经阅读过Eclipse中的某种包系统,但到目前为止还没有找到如何使用它的运气。 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. 我对此答案有用的东西如下:确保将图像/文件放在同一个eclipse文件夹(或下面的子文件夹)作为源代码。 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_ignored”,将其添加到构建路径但仍拒绝包含在我的JAR文件中(创建可执行JAR时)。

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

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

you might need to load them as class path resources if they are within a jar. 如果它们在jar中,您可能需要将它们作为类路径资源加载。 see: getClass().getClassLoader().getResourceAsStream(...) 请参阅: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. 如果您使用的是JDK 1.7或JDK 1.8,则可能需要使用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. 如果将此代码输入到返回java.nio.file.FileSystem的方法中,则可以调用该方法来获取JAR文件的FileSystem。

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. 要获取访问JAR文件中文件的路径,可以使用以下方法,然后允许您读取您可能需要的文件。

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. 如果您希望能够在Eclipse中运行它,请确保使用try / catch IOException包围对该方法的调用,并为您的FileSystem对象分配以下内容。

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. 这将允许您运行程序,无论它是否压缩为JAR文件。


I recommend you get used to using NIO.2, since it is a very powerful API. 我建议你习惯使用NIO.2,因为它是一个非常强大的API。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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