我有一个包含所有图像的文件夹,/res/来访问这些图像,我想使用我的类(class)的资源文件夹。 所以我用
getClass().getResource("/res/image.png");
我也尝试过
getClass().getResourceLoader("/res/image.png");
我也尝试过同样的事情,但在 res 前面没有/。
为了检查类路径,我做了 Sytem.out.println
System.out.println(this.getClass().getResource(""));
它返回了我的 bin 文件夹的路径
每当我尝试从/res 文件夹中获取图像时,它总是返回 null。 在 Eclipse 中,res 文件夹被指定为类文件夹。
我该如何解决这个问题?
最佳答案
在 Eclipse 中,声明为源文件夹的文件夹的内容放置在类路径的根目录中。换句话说
/res // could be called anything
/image.png
将出现在
/ (classpath root)
/image.png
这样您就可以访问它
getClass().getResource("/image.png");
或者,将其放在下面
/res
/res
/image.png
如果您想保持原样。
关于java - 加载图像作为资源返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20976801/