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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:02:11+00:00 2026-05-26T07:02:11+00:00

Hello stackoverflowites, I am in the process of developing a 2d game, and when

  • 0

Hello stackoverflowites,

I am in the process of developing a 2d game, and when I run it inside of eclipse, my program loads all of it’s images and resources fine. However, I am using the not-necessarily-recommended way of creating my images and image icons, which is shown below:

bannerLogo.setIcon(new ImageIcon("/res/client/BannerHeader.jpg"));

Now, when exporting to a jar, it does not show the image (which is expected since im not using the correct way.)

I searched for the correct way to do it, which I found was:

URL imgURL = getClass().getResource("/res/client/BannerHeader.jpg");
Image bannerImg = Toolkit.getDefaultToolkit().getImage(imgURL);
bannerLogo.setIcon(new ImageIcon(bannerImg));

And that didn’t work either, I get an “Uncaught error fetching image: ” trace, which just tells me that my URL (imgURL) is null. (This is when I run inside of eclipse, mind you, I havn’t even exported it yet)

I figure it has something to do with my classpath,however I cannot figure out what.

Inside of eclipse, my package structure is as follows:

(Parent Directory, project name)

+src folder, has normal packages, etc, on build path

+resource folder, (“res”), not on build path (tried it on build path, nothing changed)

+++subdirectories of resource folder

Im at a loss of what to do here guys. Sorry for the wall of text.

  • 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-26T07:02:12+00:00Added an answer on May 26, 2026 at 7:02 am

    Your code was initially looking for image files, and files do not exist inside of a Jar file, just resources. So you were correct to try using getClass.getResource("/res/client/BannerHeader.jpg");. But make sure that the image is in fact in the jar file in a directory off of the class file directory, and use a path that is relative to the class path directory.

    The error isn’t telling you that this is wrong or that the file is null, but rather you need to place this in a try/catch block. I suggest that you read the Exceptions Tutorial for more on this.

    Edit 1
    When you stated you had an uncaught exception error, it seemed to me that you had a compilation error, not a run-time exception. Sorry for the confusion.

    As for where to place the images, it somewhat depends on what IDE you’re using. I use Eclipse, and I add a directory off of my java file package directory that I call images and place my images there. I then use resources to look for “images/MyImage.jpg”.

    For example, say my packages in Eclipse look like so:

    Eclipse Package Explorer Image

    So the class files are located in the myPackage package, and the image file, GridBoxClassPic.JPG is located in the images directory off of the class file directory, so I can find it using the String String RESOURCE_PATH = "images/GridBoxClassPic.JPG";.

    This code could show the image:

    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    public class MyClass {
       private static final String RESOURCE_PATH = "images/GridBoxClassPic.JPG";
    
       public MyClass() {
          try {
             BufferedImage image = ImageIO.read(getClass().getResource(RESOURCE_PATH));
             ImageIcon icon = new ImageIcon(image);
             JLabel label = new JLabel(icon);
             JOptionPane.showMessageDialog(null, label);
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    
       public static void main(String[] args) {
          new MyClass();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello all you helpful folks @ stackoverflow! Best resources for Java GUI's? Looking at
Hello I am compiling a program with make but I get the error of
Hello all in my iphone application after sending login xml request am getting following
Hello is there a way to use svn command line from eclipse? Within eclipse
hello i want to add param value to all the object in a page.
Hello I am in the process of trying to colorize and swap colors on
hello all i have one json object like {event1:{title:My birthday,start:12\/27\/2011 10:20 ,end:12\/27\/2011 00:00 },event2:{title:My
Hello and Happy New Year all! While thinking about new project I decided to
Hello All I am trying to flatten a list in Ocaml. I am a
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc

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.