0

I saw that this question has been asked many times, but I didn't find any solutions that works in my case.

I am trying to load an image and display it on a JDialog. That works when I am running the app from Eclipse, but when I generate the executable JAR (with Maven), and run the JAR, the JDialog appears without any image.

I put all my class in /src/main/java, and my image is in /src/main/resources. When I unpack the JAR generated by Maven, I can find my image, so I guess the problem isn't about Maven.

Here is the code I am using to load the image (I found it here : Eclipse exported Runnable JAR not showing images) :

    URL url = myclass.class.getResource("/pic.gif");
    ImageIcon icon = new ImageIcon(url);

If it can help, that's the entire code of the class :

public class LoadingWindow {

    private JDialog dialog;
    
    public LoadingWindow() {
        this.dialog = new JDialog();
        this.dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.dialog.setTitle("Veuillez patienter");
        this.dialog.setSize(300, 200);
        URL url = LoadingWindow.class.getResource("/wait.gif");
        ImageIcon icon = new ImageIcon(url);
        JLabel imageLabel = new JLabel();
        imageLabel.setIcon(icon);
        imageLabel.setHorizontalAlignment(JLabel.CENTER);
        imageLabel.setVerticalAlignment(JLabel.CENTER);
        this.dialog.getContentPane().add(imageLabel);
        this.dialog.setLocationRelativeTo(null);
        this.dialog.setVisible(true);
    }
}

Thanks !

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
VassiliKan
  • 51
  • 1
  • 7
  • Are you getting an exception? – Reto Höhener Dec 24 '20 at 17:07
  • This code doesn't raise any exception, and if I try to test if icon is null, the code inside the conditional statement is dead (I guess because icon can't be considered as a null variable) – VassiliKan Dec 24 '20 at 17:57
  • Difficult to say without details, but based on your info I would guess that your problem is not with the resource loading, but somewhere how you use the icon. Next thing I would do is add a bunch of print statements to test assumptions at various places. – Reto Höhener Dec 24 '20 at 18:42
  • And then the usual: Reduce your code step by step to the bare minimum until it either starts working or you find the area where things start to go wrong. – Reto Höhener Dec 24 '20 at 18:44

2 Answers2

0

Is your jar an executable jar? The jar that generated through maven is called a thin jar and not an executable jar. When you want to run your app through jar, its must be an executable jar. If its not executable jar, then add below plugin to your pom.xml and build the project. In your target folder there would be 2 jars generated. Use the "jar-with-dependencies.jar".

<build>
   <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>true</addClasspath>
                  <mainClass><Your fully qualified main class name></mainClass>
                </manifest>
              </archive>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
          </plugin> 
   </plugins>
Dharman
  • 30,962
  • 25
  • 85
  • 135
P.Sanjay
  • 303
  • 1
  • 7
0

If it can helps anyone, this solution works for me :

I've created a new repository in /src/main/resources (called images), in which I put my image.

Then, I just load the image this way :

    URL url = LoadingWindow.class.getResource("/DirInResources/YourPic.png");
    ImageIcon icon = new ImageIcon(url);
VassiliKan
  • 51
  • 1
  • 7