0

I want to package images in an executable .jar file in Eclipse, but even after storing them and linking to them, they do not seem to show up. I am trying to follow this guide as it seems to be the closest to what I am trying to do.

From my understanding, I need to read each of the 4 image files as a BufferredImage from an InputStream, but then need to change them back to regular Images so they work properly within JavaFX. I have tried many variations, but this one which should seem to work best is giving me a java.lang.reflect.InvocationTargetException, so it does not even work in Eclipse properly. I know there are similar questions but none seem to answer my problem specifically (most deal with ImageIcons and JFrames) or I can't piece them together properly.

public class CardPane extends Pane
{

    private static Image[] images;
    private static BufferedImage img1;
    private static BufferedImage img2;
    private static BufferedImage img3;
    private static BufferedImage img4;
    private static Image image1;
    private static Image image2;
    private static Image image3;
    private static Image image4;

    static
    {
        try {
            img1 = ImageIO.read(ResourceLoader.getImage("green-club-100.png"));
            img2 = ImageIO.read(ResourceLoader.getImage("pink-heart-100.png"));
            img3 = ImageIO.read(ResourceLoader.getImage("black-spade-100.png"));
            img4 = ImageIO.read(ResourceLoader.getImage("blue-diamond-100.png"));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        image1 = SwingFXUtils.toFXImage(img1,null);
        image2 = SwingFXUtils.toFXImage(img2,null);
        image3 = SwingFXUtils.toFXImage(img3,null);
        image4 = SwingFXUtils.toFXImage(img4,null);
        images[0] = image1;
        images[1] = image2;
        images[2] = image3;
        images[3] = image4; 
    }
}

final public class ResourceLoader {

    public static InputStream getImage(String path) {
        InputStream input = ResourceLoader.class.getResourceAsStream(path);
        if (input == null)
            input = ResourceLoader.class.getResourceAsStream("/" + path);
        return input;
    }

}

Here is what it looks like on the file explorer. I configured the build path to have the res folder marked as the source folder, which I believe is what I am supposed to do.

Is there a quick fix to get rid of the invocation error, or am I going about this whole thing entirely wrong? Sorry if the question is confusing or seems stupid, I'm pretty new to coding and to this site. Any help would be greatly appreciated, and if any more code needs to be seen, I will be glad to share! Thanks!

Edit: I have tried to different ways with the advice in mind, but neither seem to work. They run in Eclipse fine and on my computer, but the images will not show up when the jar is opened on other devices. Here is the first with the resource loader

private static Image[] images = new Image[4];
private static Image image1 = new Image(ResourceLoader.getImage("green-club-100.png"));
private static Image image2 = new Image(ResourceLoader.getImage("pink-heart-100.png"));
private static Image image3 = new Image(ResourceLoader.getImage("black-spade-100.png"));
private static Image image4 = new Image(ResourceLoader.getImage("blue-diamond-100.png"));

static
{
    images[0] = image1;
    images[1] = image2;
    images[2] = image3;
    images[3] = image4; 
}

And the second without, using URLs as in example 2

public static URL url1 = CardGridPaneTest.class.getResource("green-club-100.png");
public static URL url2 = CardGridPaneTest.class.getResource("pink-heart-100.png");
public static URL url3 = CardGridPaneTest.class.getResource("black-spade-100.png");
public static URL url4 = CardGridPaneTest.class.getResource("blue-diamond-100.png");
public static Image image1 = new Image(url1.toString());
public static Image image2 = new Image(url2.toString());
public static Image image3 = new Image(url3.toString());
public static Image image4 = new Image(url4.toString());

public static Image[] images = new Image[4];
static {
images[0] = image1;
images[1] = image2;
images[2] = image3;
images[3] = image4;
}
Ryzcheese
  • 11
  • 4
  • *"From my understanding, I need to read each of the 4 image files as a BufferredImage from an InputStream, but then need to change them back to regular Images so they work properly within JavaFX."* Not really. `Image` provides [a constructor taking `InputStream`](https://openjfx.io/javadoc/12/javafx.graphics/javafx/scene/image/Image.html#%3Cinit%3E(java.io.InputStream)) In addition to one [you could pass the resource url (converted to string) to](https://openjfx.io/javadoc/12/javafx.graphics/javafx/scene/image/Image.html#%3Cinit%3E(java.lang.String)). – fabian Aug 09 '19 at 09:20
  • @fabian I edited my post to show you the two different ways that I tried implementing your advice, but neither seem to work. Is it strange that they show up when I run the jar on my computer, but not when others try to open the jar from my website? Makes me think things are still being loaded locally or there is another error entirely. – Ryzcheese Aug 09 '19 at 16:23
  • Try resource paths starting at the classpath root: `CardGridPaneTest.class.getResource("/green-club-100.png")`. For some version of java the paths relative to classes stopped working for me... – fabian Aug 09 '19 at 21:18
  • No luck :/ Thanks for the help though. I swear I have tried everything and followed Youtube/stackoverflow examples, I really have no idea what could be wrong... – Ryzcheese Aug 10 '19 at 00:18
  • Here's one more thing you could try: replace the `-`s with `_`s in the file names and resource paths. According to the following document at least for java 8 you need to use a file name that also would be a valid identifier: https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html#res_name_context – fabian Aug 10 '19 at 07:09

1 Answers1

0

The code is fine. You are getting that exception because you didn't initialize the images array. Define images as;

Image[] images = new Image[4]. 
Jason
  • 5,154
  • 2
  • 12
  • 22
  • Thank you but no luck. I still get the same errors. This did fix another failed attempt I had, but the images do not appear in that .jar either. – Ryzcheese Aug 09 '19 at 02:56
  • How are you jarring your application? Do the images exist within the resources folder (src/main/resources)? – Jason Aug 09 '19 at 03:06
  • I added a picture to show how the files appear in the file explorer window. I am exporting as a runnable jar file, and packaging the required libraries into the jar. – Ryzcheese Aug 09 '19 at 03:37