java relative path

可紊 提交于 2019-11-29 16:43:18
Hauke Ingmar Schmidt

getResource searches via the classloader, so typically and simplified in the classpath. The src folder is not in the classpath – it only exists for the build. Depending on your build system (ANT, Maven, IDE internal) a resources folder may be merged into the classpath. You put your resource directly into the source folder which will also work (if the build process copies all non-Java resources to the class output folder or if the source folder is used for the output of the generated classes).

/ is the root for your resources if you use absolute resource locations. It is equivalent to the root within the src folder. /i3d/resourcesloader/libraries/lib.txt would be the correct way to access the resource.

It would be nicer to separate the resources in a separate folder that is merged by the build tool (e.g. in Maven: /src/main/java, /src/main/resources).

It seems you are using NetBeans?

Just create a folder called src\resources, get your files inside there, and call this.getClass().getResource("lib.txt");.

Netbeans will pack that properly when building and the resources will be in the main folder inside the jar, so you can get without having to specify their folder.

You should only provide relative path from your classloading point (ie without src):

String path = ResourcesLoader.class.getResource("/i3d/resourcesloader/libraries/lib.txt").toString();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!