How to export java project from eclipse to jar file with images

I searched before asking and none of these techniques work. I tried putting images in a package and that also failed. So how can I successfully export a jar file from Eclipse that includes images?

+3


source to share


3 answers


MAVEN - my suggestion

If you follow the standard Maven project directory structure, it is best to place all non-Java resources in src / main / resources. For example, you can create image subdirectories such that the full path is src / main / resources / images. This directory will contain all of your application images.

Particular attention should be paid to proper handling of images when packaging an application. For example, the following function should do whatever you want.



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 like getImage ("images / some-image.png") to load the some-image.png file into the images directory.

If an ImageIcon is required, simply calling the new ImageIcon (getImage ("images / some-image.png")) will do the trick.

+1


source


In Eclipse: You can place image files along with Java source code files if you put them in a different directory, then create that directory as src directory. And create packages the same way as the source code of the code, and copy the image files into the packages you created.

For example:



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

      

Then try exporting this project as a jar file.

0


source


You need to put the images inside the source folder and make sure the settings for that folder do not exclude what you are trying to export.

Try setting up a clean Java project to make sure it all works with your version of eclipse to see what is missing. Create a new project following these instructions using the project dialog:

  • File

    > New

    >Java Project

  • In the first step of the dialog Project layout

    > Configure default...

    change the source folder name to src/main/java

    , which is a convention followed by tools like gradle and maven, so this is the project layout for most open source projects, a good convention to stick to.
  • The second step of the dialog Create new source folder

    gives it a name src/main/resources

    , which is again the standard naming.

Create a new project and drag the image into src/main/resources

eg. test.jpg

then add java class:

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 if the image file size gives. Then make the File

> export

> JAR file

and export it in /tmp/test.jar

and run it with the help of java -cp /tmp/test.jar

, to see that it gives you a way out.

Everything works for me. Go back to those project settings and compare it to the one you are trying to set up. Note that there are options to include / exclude files that are exported by filename, so it may be that you have an export filter that stops the files you export. My recommendation is that you put all your code under src/main/java

, and all resources like images and config files under src/main/resources

, then don't have export filters like build tools like maven and gradle do.

0


source







All Articles