我如何调试资源路径加载或一般的 ClassLoader?
How can I debug resources path loading or the ClassLoader in general?
我已经阅读了一些在加载资源时遇到问题的人提出的问题。我已按照他们的说明进行操作(尽管这些说明实际上有所不同,这意味着两者都不正确 - 我都试过了)。
我创建了 enum
以便在需要时为我加载资源。它很长,但我会分享它,以防有人从 google 来到这里并可以利用它:
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]( 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;
}
}
Full version on GitHub. 可能会发生变化。
由于此代码不起作用(由于无效的基本路径)我想知道找到为什么资源没有加载和的一般方法ClassLoader
在哪里看.
例如,当我从文件系统加载普通文件时遇到问题,我可以这样做:
File relativePath = new File("../my_test_image.png");
System.out.println(relativePath.getAbsolutePath());
而且我可以立即看到 Java 在看哪里以及我应该更改什么。如果我和其他人知道一个简单的方法来使用资源,那么所有这些问题都不需要问:
- Getting a BufferedImage as a resource so it will work in JAR file
- loading BufferedImage with ClassLoader.getResource()
- java getResource() not working
- Resource loading in Java not working as it should
- How to correctly get image from 'Resources' folder in NetBeans
那么有没有办法打印我的资源路径转换成什么?
我尝试了什么:
((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs();
System.out.println(" Current path: \""+loader.getResource("ImageResources.class")+"\")");
- 打印 Current path: "null"
看来你总能通过这条语句得到当前路径:
System.out.println(" Path: \""+loader.getResource(".")+"\"");
对我来说,这给出了:
Path: "file:/C:/... path to project .../PROJECT_NAME/target/test-classes/"
这是测试的路径 class 运行 测试,而不是实际的 class loader
创建它的路径。我不认为这是一个错误。
我已经阅读了一些在加载资源时遇到问题的人提出的问题。我已按照他们的说明进行操作(尽管这些说明实际上有所不同,这意味着两者都不正确 - 我都试过了)。
我创建了 enum
以便在需要时为我加载资源。它很长,但我会分享它,以防有人从 google 来到这里并可以利用它:
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]( 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;
}
}
Full version on GitHub. 可能会发生变化。
由于此代码不起作用(由于无效的基本路径)我想知道找到为什么资源没有加载和的一般方法ClassLoader
在哪里看.
例如,当我从文件系统加载普通文件时遇到问题,我可以这样做:
File relativePath = new File("../my_test_image.png");
System.out.println(relativePath.getAbsolutePath());
而且我可以立即看到 Java 在看哪里以及我应该更改什么。如果我和其他人知道一个简单的方法来使用资源,那么所有这些问题都不需要问:
- Getting a BufferedImage as a resource so it will work in JAR file
- loading BufferedImage with ClassLoader.getResource()
- java getResource() not working
- Resource loading in Java not working as it should
- How to correctly get image from 'Resources' folder in NetBeans
那么有没有办法打印我的资源路径转换成什么?
我尝试了什么:
((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs();
System.out.println(" Current path: \""+loader.getResource("ImageResources.class")+"\")");
- 打印Current path: "null"
看来你总能通过这条语句得到当前路径:
System.out.println(" Path: \""+loader.getResource(".")+"\"");
对我来说,这给出了:
Path: "file:/C:/... path to project .../PROJECT_NAME/target/test-classes/"
这是测试的路径 class 运行 测试,而不是实际的 class loader
创建它的路径。我不认为这是一个错误。