0

I have this issue where my program works fine in eclipse, correctly creating images to be made into JLabels, but when I try to export my program into a runnable jar, it no longer works. I've done some debugging and managed to figure out that the method I have for creating the images returns does not work after exporting. I assume this is because making the program a runnable jar file changes the paths of the files, but I'm left clueless as to how to fix it. Relevant details below, any help is appreciated!

// This is the method that calls the method to create the pictures
public static void buildMapper() {
        readPics();
    ...

    // Create JLabel pictures
        JLabel f1 = new JLabel(new ImageIcon(floor1.getScaledInstance(1275, 825, Image.SCALE_SMOOTH)));
    ..
}

private static void readPics() {
        try {
            // Load floors
            floor1 = ImageIO.read(new File("src\\resources\\floor1.png"));
      ...
    }catch(IOException ex){ System.out.println("IO Exception"); }
}
// floor1 is a private static BufferedImage

Here is how I've set up my project tree. The resources folder just contains all the images I've used, like floor1.png Project Explorer

Again, any help would be greatly appreciated! This is the first time I've done a programming project to this big of a scope, involving a lot of things I had to learn myself, so there's bound to be plenty of errors to learn from!

edit: I managed to fix it. If anyone else is struggling, my solution was to change the line floor1 = ImageIO.read(new File("src\\resources\\floor1.png")); to floor1 = ImageIO.read(getClass().getResource("/resources/floor1.png"));. I also had to make the method not static, since this.getClass() cannot be called in a static method.

breezy23_
  • 1
  • 1

0 Answers0