Having issue accessing images inside jar (java swing)

33 Views Asked by At

First I know that there already exists lots of topics on how to access the image inside the jar file.I tried many options and none of them worked. I have created one java swing application using Intellij IDE. The app has many images used in the panels as well as tables. These images are displayed properly when I run my project inside Intellij. But when i create the JAR it executes fine but the images are missing. However, when I put the images in the same directory as the JAR file the images are properly displayed.Is there a way to package everything in a JAR? I packaged Sounds folder for now and it actually works for the sounds but for images it doesn't. 1 MIN VIDEO OF HOW IT LOOKS: https://www.youtube.com/watch?v=D4lCRljlZqY&ab_channel=GMdz0ji

I looked many answers on the Internet and nothing worked for me. I am displaying image like this ImageIcon icon = new ImageIcon("Images/logoNewLogin.png"); Here is my project structure

- src
        - main
                - java
                - resources
                      - Images/  (multiple images)
                      - Sounds/  (multiple .mp4 sounds)

Also lots of answers for obtaining the image which i saw were something like this (maybe i did this wrong but this was indeed an answer)

class A{
public static void main(String[] args){                                                                         ImageIcon image = new ImageIcon();
    try {
      image.setImage((Toolkit.getDefaultToolkit().getImage(A.class.getResource("/images/"+imageName))));
    }
    catch(Exception ex) {
      System.out.println("Image Error: " + ex);
      ex.printStackTrace();
    }
}
0

There are 0 best solutions below