为什么我的 `getClass().getResource("logs")` returns 为空?

why is my `getClass().getResource("logs")` returns null?

我有这个代码:

this.fileName = getClass().getResource("logs").toURI().toString();

因空指针异常而失败,因为 getClass().getResource("logs") returns null

我错过了什么?

您正在寻找与 LogUtils class.

相关的资源

您可以使用 getClass().getResource("/logs") 或者您可以使用 getClass().getClassLoader().getResource("logs").

(无论哪种情况,我都不能 100% 相信当它是文件夹而不是实际资源时它会起作用...)