How to export jar with images on Eclipse?

徘徊边缘 提交于 2019-12-01 06:53:31

问题


I created sort of chess game (it isn't exactly chess but I don't know how it called in English) and I want to export it as runnable jar.

The problem is that images (at this program - the players) are not exported for some strange reason.

How to export runnable jar on eclipse with images? thanks.


回答1:


The recommended way would be to have a resource directory under your project root and include it into the list of source code directories. This will result in all images there being copied into the JAR. If you make a subdirectory there, resource/image, then you'll end up with a JAR that has an image directory. You access those images through the classloader:

classloader.getResourceAsStream("/image/name.jpg");

or, whenever you pass the image to an API that accepts resource URLs:

classloader.getResource("/image/name.jpg");

Of course, this is all subject to how exactly you build your JAR, but if you do it through Eclipse's Export JAR, you'll be able to achieve what I'm describing. If you use Maven, there is a quite similar approach to the one I described.

Note also that I deliberately avoid demonstrating code that fetches the classloader as this is a non-trivial subject in Java and should be done in a context-specific way. However, if you do it from a class that is in the same JAR as the images, it is a safe bet that this will work from an instance method:

this.getClass().getClassLoader();

this is optional here, and is actually not recommended from the code style perspective, but I included it for clarity and because it would be wrong and dangerous to call getClass on an instance of any class other than your own.




回答2:


Let me put a couple of examples in case you may find them interesting:

To write the resource (image) from jar file into a DataOutPutStream:

public static void readResourceFromJarToDataOutputStream(String file,
        DataOutputStream outW) {
    try {
        InputStream fIs = new BufferedInputStream(new Object() {
        }.getClass().getResourceAsStream(file));
        byte[] array = new byte[4096];
        for (int bytesRead = fIs.read(array); bytesRead != -1; bytesRead = fIs
                .read(array)) {
            outW.write(array, 0, bytesRead);
        }
        fIs.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

To load the resource in memory (byte array):

public static byte[] readResourceFromJarToByteArray(String resource) {
    InputStream is = null;
    byte[] finalArray = new byte[0];
    try {
        is = new Object() {
        }.getClass().getResourceAsStream(resource);
        if (is != null) {
            byte[] array = new byte[4096];//your buffer size
            int totalBytes = 0;
            if (is != null) {
                for (int readBytes = is.read(array); readBytes != -1; readBytes = is
                        .read(array)) {
                    totalBytes += readBytes;
                    finalArray = Arrays.copyOf(finalArray, totalBytes);
                    System.arraycopy(array, 0, finalArray, totalBytes- readBytes, 
                            readBytes);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return finalArray;
}



回答3:


Simply put ALL of the resources, such as images, text files, EVERYTHING into the directory where the runnable Jar will be. It solved the problem for me.



来源:https://stackoverflow.com/questions/12881123/how-to-export-jar-with-images-on-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!