4

Folks,

I am developing a Java application using Eclipse. Maven is used to create the final jar file.

In the application, I use some image icons for the buttons. Following some instructions on the Internet, I created a "source" directory by clicking on the project. I named the source directory as "res" and moved my images to this directory.


public static ImageIcon getIcon() {
  if (isThisJarfile()) {
     URL url = this.class.getResources("/res/myicon.png");
     return new ImageIcon(url);
  }else {
     return new ImageIcon("/res/myicon.png");
  }
}

This works fine when the app is not packaged as a jar file (great for debugging). However, when maven packages it, I see that the images are put in the root directory of the jar file. The following call works:

    URL url = this.class.getResource("/myicon.png");

I am wondering if there is some step that I overlooked.

Note that I didn't have to do anything special to pom.xml for the images. Maven automatically picked them up (except that it is putting them in the wrong location).

Thank you in advance for your help.

Regards, Peter

Peter
  • 11,260
  • 14
  • 78
  • 155

3 Answers3

7

If you're following the standard Maven project directory structure then it is best to put all non-Java resources under src/main/resources. For example, you could create a subdirectory images, so that the full path would be src/main/resources/images. This directory would contain all your application images.

A special care should be taken to properly access images when application is packaged. For example, the following function should do everything you need.

public static Image getImage(final String pathAndFileName) {
    final URL url = Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
    return Toolkit.getDefaultToolkit().getImage(url);
}

This function can be used as getImage("images/some-image.png") in order to load some-image.png file in the image directory.

If ImageIcon is required then simply calling new ImageIcon(getImage("images/some-image.png")) would do the trick.

01es
  • 5,362
  • 1
  • 31
  • 40
  • This worked perfectly both inside and outside the jar. Thank you very much for your help. Regards. Peter – Peter May 06 '12 at 00:03
  • But why place it under resources instead of webapp? Placing it under resources will place the images in the class file under WEB-INF which isn't right because these are static files. – robben May 24 '17 at 13:31
0

I had my favicon in the root of maven project and it was not getting included in the generated war. With lot of googling I got the solution from maven help page.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
  <webResources>
    <resource>
        <directory>${project.basedir}</directory>
      <!-- the list has a default value of ** -->
      <includes>
        <include>favicon.ico</include>
      </includes>
    </resource>
  </webResources>
</configuration>
</plugin>
tanson
  • 82
  • 3
-1

Take a look at maven resources plugin

Anton
  • 5,831
  • 3
  • 35
  • 45