Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:09:11+00:00 2026-06-03T07:09:11+00:00

Folks, I am developing a Java application using Eclipse. Maven is used to create

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:09:12+00:00Added an answer on June 3, 2026 at 7:09 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, I've got an ASP.NET MVC application that I am attempting to secure using
I am developing one web application using (PHP JavaScript). I stuck to one point
hi folks i am developing an sqlite application in iphone. since i am new
Folks, We have been using OpenXml APIs found in System.IO.Packaging for creating a package
Folks, I really like the -Wshadow option since it helps to spot some possible
Folks, I'm tryning to extract data from web page using C#.. for the moment
folks, i need to retrieve colors of an image.. how to retrieve it?
Folks, I need to maintain a C#/.Net desktop application. So, I need to set
I'm developing a phone app and need a modal dialog with some rich content
Folks, I'm trying to open a youtube video in fancybox using the following code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.