简体   繁体   English

如何将eclipse中的java项目导出到带有图像的jar文件中

[英]How to export java project from eclipse into a jar file with images

I searched before asking and none of those techniques work. 我在询问之前进行了搜索,但这些技术都没有用。 I tried to put images under package, and this failed too. 我试图将图像放在包装下,这也失败了。 So how can I successfully export the jar file from Eclipse which includes images? 那么如何从包含图像的Eclipse成功导出jar文件呢?

MAVEN is my suggestion MAVEN是我的建议

If you're following the standard Maven project directory structure then it is best to put all non-Java resources under src/main/resources. 如果您遵循标准的Maven项目目录结构,那么最好将所有非Java资源放在src / main / resources下。 For example, you could create a subdirectory images, so that the full path would be src/main/resources/images. 例如,您可以创建子目录图像,以便完整路径为src / main / resources / images。 This directory would contain all your application images. 该目录将包含您的所有应用程序映像。

A special care should be taken to properly access images when application is packaged. 在打包应用程序时,应特别注意正确访问图像。 For example, the following function should do everything you need. 例如,以下函数应该执行您需要的所有操作。

public static Image getImage(final String pathAndFileName) {
    final URL url = Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
    return Toolkit.getDefaultToolkit().getImage(url);
}

This function can be used as getImage("images/some-image.png") in order to load some-image.png file in the image directory. 此函数可用作getImage(“images / some-image.png”),以便在image目录中加载some-image.png文件。

If ImageIcon is required then simply calling new ImageIcon(getImage("images/some-image.png")) would do the trick. 如果需要ImageIcon,那么只需调用new ImageIcon(getImage(“images / some-image.png”))即可。

In Eclipse: You can place image files along with Java source code files, if you are putting it in other directory then make sure you create that directory as src directory. 在Eclipse中:您可以将图像文件与Java源代码文件一起放置,如果您将其放在其他目录中,请确保将该目录创建为src目录。 And create packages same as source code packages, and copy image files to those created packages. 并创建与源代码包相同的包,并将图像文件复制到那些创建的包。

For example: 例如:

  src (source directory)
       ^-------------------com.myproject
                                ^................ Main.java
  resource (source directory)
       ^-------------------com.myproject
                                ^................ logo.png

Then try to export this project as jar file. 然后尝试将此项目导出为jar文件。

You need to put your images inside of a source folder and ensure that the settings for that folder don't exclude what you are trying to export. 您需要将图像放在源文件夹中,并确保该文件夹的设置不会排除您尝试导出的内容。

Try setting up a clean Java project to ensure that it all works as expected with your version of eclipse to see what may be missing. 尝试设置一个干净的Java项目,以确保它与您的eclipse版本一样正常工作,以查看可能缺少的内容。 Create a new project following these instructions using the project dialog: 使用项目对话框按照这些说明创建一个新项目:

  1. File > New > Java Project File > New > Java Project
  2. In the first dialogue step Project layout > Configure default... change the source folder name to src/main/java which is the convention followed by tools like gradle and maven so it is the project layout for most opensource projects so its a good convention to stick with. 在第一个对话步骤中, Project layout > Configure default...将源文件夹名称更改为src/main/java ,这是常规,然后是gradle和maven等工具,因此它是大多数开源项目的项目布局,因此它是一个很好的约定坚持下去。
  3. Second dialogue step Create new source folder give it the name src/main/resources which is again the standard naming. 第二个对话步骤Create new source folder给它命名为src/main/resources ,这又是标准命名。

Create the new project and drag-drop an image into src/main/resources eg test.jpg then add a java class: 创建新项目并将图像拖放到src/main/resources例如test.jpg然后添加一个java类:

public class Test {
    public static void main(String[] args) throws Exception {
        System.out.println(Test.class.getClassLoader().getResourceAsStream("test.jpg").available());
    }
}

Run it in eclipse to check it gives you the image file size. 在eclipse中运行它来检查它给你的图像文件大小。 Then do File > export > JAR file and export it to /tmp/test.jar and run it with java -cp /tmp/test.jar to see that it gives you the output. 然后执行File > export > JAR file并将其导出到/tmp/test.jar并使用java -cp /tmp/test.jar运行它以查看它是否为您提供输出。

That all works for me. 这一切都适合我。 Go back through that project settings and compare it with the one you are trying to setup. 返回项目设置并将其与您尝试设置的项目设置进行比较。 Note that there are options to include/exclude files which get exported by filename so it may be the case what you have an export filter which is stopping your files being exported. 请注意,有一些选项可以包含/排除按文件名导出的文件,因此可能会出现导出过滤器停止导出文件的情况。 My recommendation is that you put all code under src/main/java and all resources such as images and config files under src/main/resources then have no export filters which is how build tools such as maven and gradle do things. 我的建议是你把所有代码放在src/main/java ,所有资源如src/main/resources下的图像和配置文件都没有导出过滤器,这就是构建工具如maven和gradle的工作方式。

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

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