I have a project where I’m compiling files to locations relative to getClass().getResource("/"). How can I get File objects for these locations?
When I try getClass().getResource("/nonExisting"), they return null. How can I resolve the paths?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s no reason to expect existent resources to be normal files; they could be buried inside a jar-file somewhere on your classpath.
With non-existent resources, the situation is even worse; even if you could guarantee in the abstract that it is a non-existent normal file, it could be a non-existent normal file in any of the top-level directories in your classpath; there’s no sensible way to decide which directory it would have been in, had it existed. (For example, if your classpath contains both
classesandtestClasses, then/nonExistingcould be eitherclasses/nonExistingortestClasses/nonExisting.)