In java play framework project I do not figure out in retrieving file sample.txt in same package of my current controller class.
I try with this command:
URL resource = MyController.class.getResource("text.txt");
This is my current project structure:
- app
---- test
--------- controller
------------- MyController
------------- text.txt
I have tried with a similar command:
InputStream resource = MyController.class.getResourceAsStream("text.txt");
or
ClassLoader cl = getClass().getClassLoader();
File file = new File(cl.getResource("text.txt").getFile());
Same null pointer.
How can I solve it?
Maybe the instruction: cl.getResource("text.txt").getFile()
in play framework do not get the file in the path getClass().getClassLoader()
. It can be a visibility problem. Play framework can not access to all project folder via getClassLoader(). Try to put the file into conf folder and get it with Play.application().resourceAsStream("file.txt");
Controller directory it is not the execution directory, it is only for the source code files.
Work directly with ressources, for your case, intenal usages, see https://www.playframework.com/documentation/2.8.x/sbtCookbook#Configure-externalized-resources
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.