简体   繁体   中英

How to export jar with images on Eclipse?

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.

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.

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;
}

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.

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