简体   繁体   中英

Can't load an image when running a jar from maven project

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 !

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>

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

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