Java 如何调试资源路径加载或类加载程序?

Java 如何调试资源路径加载或类加载程序?,java,netbeans,embedded-resource,Java,Netbeans,Embedded Resource,我读到了一些在加载资源时遇到问题的人提出的问题。我遵循了他们的指示,虽然这些指示实际上不同,这意味着要么是错误的-我都试过了 我创建了enum,以便在需要时为我加载资源。它很长,但我会分享它,以防有人从谷歌来到这里,可以利用它: package cz.autoclient.GUI; /** * Enum of resources used for GUI. These resources are packed in .jar and are for internal use. * Provi

我读到了一些在加载资源时遇到问题的人提出的问题。我遵循了他们的指示,虽然这些指示实际上不同,这意味着要么是错误的-我都试过了

我创建了enum,以便在需要时为我加载资源。它很长,但我会分享它,以防有人从谷歌来到这里,可以利用它:

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;
  }
}
可能会发生变化

由于此代码由于基路径无效而无法工作,因此我想知道一种通用方法,以找到为什么不加载资源以及类加载器在哪里

例如,当我从文件系统加载普通文件时遇到问题,我可以这样做:

File relativePath = new File("../my_test_image.png");
System.out.println(relativePath.getAbsolutePath());
我可以立即看到Java在看什么,我应该改变什么。如果我和其他人都知道利用资源实现这一目标的简单方法,那么就不需要问所有这些问题:

那么,有没有一种方法可以打印我的资源路径转换为什么

我尝试的是:

-打印当前路径:空
因此,您似乎总能通过以下语句获得当前路径:

System.out.println("  Path: \""+loader.getResource(".")+"\"");
对我来说,这给了:

  Path: "file:/C:/... path to project .../PROJECT_NAME/target/test-classes/"

这是运行测试的测试类的路径,而不是为其创建的实际类装入器。我不认为这是一个bug。

URLClassLoader-Thread.currentThread.getContextClassLoader.getURLs;对你不起作用?那会打印出很多maven依赖项。另外,结果实际上是文件系统绝对路径:leclass.getResourceAsStream/images/grass.png将尝试通过images包中leclass的classloader加载grass.png。包需要与leclass类位于同一目录或.jar中。对我来说,它不起作用,因为我运行的是在不同目录中运行的测试类Maven。但是图书馆如何处理这个问题呢?它们在测试类目录中运行没有问题。。。