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 661021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:13:20+00:00 2026-05-13T23:13:20+00:00

I have a Java project with a toolbar, and the toolbar has icons on

  • 0

I have a Java project with a toolbar, and the toolbar has icons on it. These icons are stored in a folder called resources/, so for example the path might be “resources/icon1.png”. This folder is located in my src directory, so when it is compiled the folder is copied into bin/

I’m using the following code to access the resources.

    protected AbstractButton makeToolbarButton(String imageName, String actionCommand, String toolTipText,
        String altText, boolean toggleButton) {

    String imgLocation = imageName;
    InputStream imageStream = getClass().getResourceAsStream(imgLocation);

    AbstractButton button;
    if (toggleButton)
        button = new JToggleButton();
    else
        button = new JButton();

    button.setActionCommand(actionCommand);
    button.setToolTipText(toolTipText);
    button.addActionListener(listenerClass);

    if (imageStream != null) { // image found
        try {
            byte abyte0[] = new byte[imageStream.available()];
            imageStream.read(abyte0);

            (button).setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage(abyte0)));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                imageStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else { // no image found
        (button).setText(altText);
        System.err.println("Resource not found: " + imgLocation);
    }

    return button;
}

(imageName will be “resources/icon1.png” etc). This works fine when run in Eclipse. However, when I export a runnable JAR from Eclipse, the icons are not found.

I opened the JAR file and the resources folder is there. I’ve tried everything, moving the folder, altering the JAR file etc, but I cannot get the icons to show up.

Does anyone know what I’m doing wrong?

(As a side question, is there any file monitor that can work with JAR files? When path problems arise I usually just open FileMon to see what’s going on, but it just shows up as accessing the JAR file in this case)

Thank you.

  • 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-05-13T23:13:20+00:00Added an answer on May 13, 2026 at 11:13 pm

    To load an image from a JAR resource use the following code:

    Toolkit tk = Toolkit.getDefaultToolkit();
    URL url = getClass().getResource("path/to/img.png");
    Image img = tk.createImage(url);
    tk.prepareImage(img, -1, -1, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java project called ood_100 to which I am trying to add
I have java project that depends from other java projects. These projects are maven
I have a Java project X has dependency (in pom file) on project Y.
I have a java project in eclipse which are using tomcat. This project has
I have a Java project that currently has a lot of JARs in its
I have a Java project which has this file structure (shown in Eclipse): ProjectName
I have a Java project called MyProject. I have a few different packages (keeping
I have a Java project with all source files in the src folder. This
I have a java project that uses JPA 2/Hibernate 3.5.6 for data access and
I have a Java project that needs a addon interface. I was thinking about

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.