title: How can I debug resources path loading or the ClassLoader in general?
body: <p>I've read up some questions by people having problems loading resources. I have followed the instructions they've got (though these instructions actually differ which means either were incorrect - I tried all).</p>
  
  <p>I created <code>enum</code> to load the resources for me when needed. It's long but I'll share it in case somebody came here from google and could make use of it:</p>
  
  <pre><code>package cz.autoclient.GUI;
  /**
   * Enum of resources used for GUI. These resources are packed in .jar and are for internal use.
   * Provides lazy-loaded Image and ImageIcon for comfort.
   * @author Jakub
   */
  public enum ImageResources {
    ICON("IconHighRes.png");
    //So according to [this guy](https://stackoverflow.com/a/17007533/607407) I
    //  should enter classpath beginning with slash to make sure it's absolute path from
    //  the root of my .jar
    public static final String basepath = "/cz/autoclient/resources/";
    //Cache everything to have less letters to write
    public static final ClassLoader loader = ImageResources.class.getClassLoader();
    public static final Class leclass = ImageResources.class;
    //String is immutable so it's ok to make it a public constant
    public final String path;
    //These will fill up on demand when needed
    private ImageIcon icon;
    private Image image = null;
    //If image has failed, we'll not try to load it again and will return null straight away
    private boolean image_failed = false;
    //Constructor concatenates the individual path with the global path
    ImageResources(String path) {
      this.path = basepath+path;
    }
  
    /** Loads, or just retrieves from cache, the image.
     *  @return Image (not necesarily a BufferedImage) or null on failure
    */
    public Image getImage() {
      //Lazy load...
      if(image==null) {
        //Since the .jar is constant (it's packed) we can
        //Remember the image is unavailable
        if(image_failed)
          return null;
        //Use whatever is stored in Icon if we have it
        if(icon!=null) {
          image = icon.getImage();
        }
        //Load from .jar
        else {
          try {
            image = ImageIO.read(leclass.getResourceAsStream("/images/grass.png"));
          }
          //While only IOException is reported it also can throw InvalidArgumentException
          // when read() argument is null
          catch(Exception e) {
            image_failed = true;
          }
        }
      }
      return image;
    }
  }
  </code></pre>
  
  <p><sup><a href="https://github.com/Darker/auto-client/blob/master/src/cz/autoclient/GUI/ImageResources.java" rel="nofollow noreferrer">Full version on GitHub.</a> Can be subject to change.</sup></p>
  
  <p>Since this code doesn't work (due to invalid base path) I want to know a general way to find <strong>why are the resources not loading</strong> and <strong>where is the <code>ClassLoader</code> looking</strong>.</p>
  
  <p>For example when I had problems loading normal file from filesystem, I could do this:</p>
  
  <pre><code>File relativePath = new File("../my_test_image.png");
  System.out.println(relativePath.getAbsolutePath());
  </code></pre>
  
  <p>And I could immediately see where's Java looking and what should I change. If I as well as others know a simple way to do this with resources, all these questions wouldn't need to be asked:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/17007448/607407">Getting a BufferedImage as a resource so it will work in JAR file</a></li>
  <li><a href="https://stackoverflow.com/q/1799371/607407">loading BufferedImage with ClassLoader.getResource()</a></li>
  <li><a href="https://stackoverflow.com/q/12440035/607407">java getResource() not working</a></li>
  <li><a href="https://stackoverflow.com/q/9615150/607407">Resource loading in Java not working as it should</a></li>
  <li><a href="https://stackoverflow.com/q/6845231/607407">How to correctly get image from &#39;Resources&#39; folder in NetBeans</a></li>
  </ul>
  
  <p>So is there a way to <strong>print what does my resource path translate to?</strong></p>
  
  <p>What I tried:</p>
  
  <ul>
  <li><a href="http://www.mkyong.com/java/how-to-print-out-the-current-project-classpath/" rel="nofollow noreferrer"><code>((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs();</code></a></li>
  <li><a href="https://stackoverflow.com/a/227569/607407"><code>System.out.println(" Current path: \""+loader.getResource("ImageResources.class")+"\")");</code></a> - prints <code>Current path: "null"</code></li>
  </ul>