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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:35:21+00:00 2026-06-14T00:35:21+00:00

I understand from What is the difference between Class.getResource() and ClassLoader.getResource()? and from own

  • 0

I understand from What is the difference between Class.getResource() and ClassLoader.getResource()?
and from own code, that

getClass().getResource("/path/image.png")

is identical to

getClass().getClassLoader().getResource("path/image.png")

The posting Cannot read an image in jar file shows an issue where using

getClass().getClassLoader().getResource("path/image.png")

in an executable jar file returns null, while

getClass().getResource("/path/image.png")

returns the correct URL.

Since Class.getResource() delegates to ClassLoader.getResource() after removing the leading slash, I would expect that these calls are identical, but obviously they are not in this case. Even when a special class loader is attached to the particular class, it should still be the same one for each call, again resulting in the same behavior.

So, the question is: are there any obvious circumstances under which the following
code returns null for the first call but the proper URL for the second call?

package com.example;

import java.net.URL;

public class ResourceTest {

   public void run() {
      URL iconUrl1 = getClass().getClassLoader().getResource("path/image.png");
      System.out.println("ClassLoader.getResource(\"path/image.png\"): " + iconUrl1);

      URL iconUrl2 = getClass().getResource("/path/image.png");
      System.out.println("Class.getResource(\"/path/image.png\"): " + iconUrl2);
   }

   public static void main(String[] args) {
      ResourceTest app = new ResourceTest();
      app.run();
   }
}
  • 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-14T00:35:22+00:00Added an answer on June 14, 2026 at 12:35 am

    I thought this question was already asked and answered!

    • What is the difference between Class.getResource() and
      ClassLoader.getResource()?

    getClass().getResource() searches relative to the .class file while
    getClass().getClassLoader().getResource() searches relative to the
    classpath root.

    If there’s an SSCCE here, I don’t understand why it doesn’t

    1) Show the directory organization in the .jar, and…

    2) Take package into consideration

    Q: What (if anything) hasn’t already been answered by What is the difference between Class.getResource() and ClassLoader.getResource()? (and the links it cites)?

    =========================================================================

    I’m still not sure what isn’t clear, but this example might help:

    /*
      SAMPLE OUTPUT:
      ClassLoader.getResource(/subdir/readme.txt): NULL
      Class.getResource(/subdir/readme.txt): SUCCESS
    
      ClassLoader.getResource(subdir/readme.txt): SUCCESS
      Class.getResource(subdir/readme.txt): NULL
     */
    package com.so.resourcetest;
    
    import java.net.URL;
    
    public class ResourceTest {
    
        public static void main(String[] args) {
            ResourceTest app = new ResourceTest ();
        }
    
        public ResourceTest () {
            doClassLoaderGetResource ("/subdir/readme.txt");
            doClassGetResource ("/subdir/readme.txt");
            doClassLoaderGetResource ("subdir/readme.txt");
            doClassGetResource ("subdir/readme.txt");
        }
    
        private void doClassLoaderGetResource (String sPath) {
            URL url  = getClass().getClassLoader().getResource(sPath);
            if (url == null)
                System.out.println("ClassLoader.getResource(" + sPath + "): NULL");
            else
                System.out.println("ClassLoader.getResource(" + sPath + "): SUCCESS");
        }
    
        private void doClassGetResource (String sPath) {
            URL url  = getClass().getResource(sPath);
            if (url == null)
                System.out.println("Class.getResource(" + sPath + "): NULL");
            else
                System.out.println("Class.getResource(" + sPath + "): SUCCESS");
        }
    }
    

    Here’s the corresponding directory tree. It happens to be an Eclipse project, but the directories are the same regardless if it’s Eclipse, Netbeans … or a .jar file:

    C:.
    ├───.settings
    ├───bin
    │   ├───com
    │   │   └───so
    │   │       └───resourcetest
    │   └───subdir
    └───src
        ├───com
        │   └───so
        │       └───resourcetest
        └───subdir
    

    The file being opened is “subdir/readme.txt”


    ADDENDUM 11/9/12:

    Hi –

    I copied your code verbatim from github, re-compiled and re-ran:

    ClassLoader.getResource(/subdir/readme.txt): NULL
    Class.getResource(/subdir/readme.txt): SUCCESS
    ClassLoader.getResource(subdir/readme.txt): SUCCESS
    Class.getResource(subdir/readme.txt): NULL
    

    If that’s not the output you’re getting … I’m baffled.

    For whatever it’s worth, I’m running:

    • Eclipse Indigo (it shouldn’t matter)

    • Running inside the IDE (it shouldn’t matter if it’s filesystem or .jar, inside or outside an IDE)

    • My JRE is 1.6 (if anything, this is probably the biggie)

    Sorry we haven’t been able to resolve what I thought was a straightforward issue 🙁


    ADDENDUM 11/21/12 (Andreas):

    Since there was no recent activity on this question, I would like to summarize what we found:

    • From our common understanding, the answer to the above question is: “No, it is not possible that Class.getResource("/path/image.png") returns a valid URL, while ClassLoader.getResource("path/image.png") returns null”:
      • We’re completely clear on the difference between ClassLoader.getResource() and Class.getResource()
      • Our sample outputs match, for both “SUCCESS” and for “null”
      • The sample outputs match what we’d expect
      • Conclusion: Either we oversaw something, or something different caused the “solution” described in the linked question to work. I think we can not currently prove one or the other.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like I understand the difference between require and require_dependency (from How are
As you can understand from title, i modified Settings.cs file.Added some properties, some code
I found that Maven implies specific directory layout. But I don't understand from here:
I’m trying to understand code from http://www.yesodweb.com/book/conduits . After some fixes (like replacing Resource
I try to understand the ICommand from wpf. In my Event class I implement
I'm trying to understand the difference between OnStart() and the constructor in a ServiceBase
I don't understand the difference between protected and private members or methods, as I
I wish best understand the difference between dealloc and release function.... example... I have
I am trying to understand this particular difference between the direct and delegated event
I am trying to understand the difference between matches() and find() . According to

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.