getResourceAsStream 返回 null
我看到很多其他人在此处和此处,但似乎我已经完成了所有必要的操作,但在使用时仍然得到 null getResourceAsStream() 调用。
在 netbeans 中,我将图像拖到包中,它显示在保存源代码和 .class 文件的文件夹中。所以我有: src\com\myProject\ClassIMakeTheCallFrom.java src\com\myProject\MyImage.png
中也存在类似的结构: build\classes\com\myProject\ClassIMakeTheCallFrom.class build\classes\com\myProject\MyImage.png
但是,当我拨打电话时:
System.out.println(dolphinWater.class.getClassLoader().getResourceAsStream("myImage.png"));
它显示为空。我错过了什么吗?
I've seen many other people ask this question here and here, but it seems that I have done everything necessary yet I still get null when using the getResourceAsStream() call.
In netbeans I dragged my image into my package and it shows up in the folder in which the source code and .class files are held. So I have:
src\com\myProject\ClassIMakeTheCallFrom.java
src\com\myProject\MyImage.png
also a similar structure exists in:
build\classes\com\myProject\ClassIMakeTheCallFrom.class
build\classes\com\myProject\MyImage.png
However when I make the call:
System.out.println(dolphinWater.class.getClassLoader().getResourceAsStream("myImage.png"));
It comes out null. Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要么将文件直接放在 src 文件夹中,而不是包文件夹中。或者使用 getResourceAsStream("com/myProject/myImage.png")。
http:// /docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)
Either put the file directly in the src folder, not the package folder. Or use
getResourceAsStream("com/myProject/myImage.png")
.http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)