getClass()。getResourceAsStream(" / folder")返回null [英] getClass().getResourceAsStream("/folder") return null

查看:143
本文介绍了getClass()。getResourceAsStream(" / folder")返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用

getClass()列出jar文件夹文件.getResourceAsStream(/ folderName)它返回 null

但是 getClass()。getResourceAsStream(folderName / fileName ) null

有什么区别?

它在eclipse中有效但不在 jar 文件中

it works in eclipse but not in jar file

推荐答案

您告诉类加载器找到/ folderName中的文件(资源),它是根(绝对)。虽然folderName是您班级所在的当前文件夹(相对于)。

You're telling the Class Loader to locate the file (resource) in "/folderName" which is the root (absolute). While "folderName" is the current folder (relative to) your class is located in.

Plus:


  • getClass()。getResourceAsStream()正在使用System Class Loader。不是加载父类的类加载器。

  • getClass().getResourceAsStream() is using the System Class Loader. Not the class loader that loaded the parent class.

使用 Example.class.getResourceAsStream()和您应该能够访问资源。

Use Example.class.getResourceAsStream() and you should be able to access the resources.

这篇关于getClass()。getResourceAsStream(" / folder")返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆