defining root classpath for class.getResource()

安稳与你 提交于 2019-12-07 00:44:06

问题


I'm using java in Eclipse (windows), and I'm trying to use:

myClass.getResource("/image.jpg"), 

While image.jpg exists in the root loacation "myProject/". It returns null.

I checked many other threads but none of them resolved my issue.

So I tried to evaluate this:

this.getClass().getResource("/").getPath();

and it returned "myProject/build/classes".

So I checked the project's configuration for this dir, and found the exact same dir under

Java Build Path --> Source --> Default output folder

My questions are:

  1. why is the default dir of my resources is the output folder?

  2. As mentioned, I want to get a resource from another folder, but attempts to call "/../" aren't working... How can I then load other resources which are not nested in my classes dir? (I can change my output dir, but I think it will be very ugly...)


回答1:


You must put them in the root of "src/" folder ;-)

SRC/ folder is compiled to BUILD/CLASSES/, so all you put in src/* goes to classes/*



来源:https://stackoverflow.com/questions/19104545/defining-root-classpath-for-class-getresource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!