Julian Pieles
Julian Pieles

Reputation: 4016

Java ClassLoader getResource with special characters in path

I have the following problem. In our integration tests we use a different config which will be loaded from test resources with the following code prior to the tests:

URL resource = ClassLoader.getSystemResource("application.conf");

This works fine as long as there are no special characters in path. For example having the following correct path

D:/Dev/projects/#FLI/flinsta/fgraph/build/resources/test/application.conf

will result in the following wrong file path given by getSystemResource:

D:/Dev/projects/%23FLI/flinsta/fgraph/build/resources/test/application.conf

This then results in a file which simply doesn't exist. How can I make sure that something like this does not happen. Renaming the path is an option. However I would like to find a solution instead of a workaround.

Thank you for any help!

Upvotes: 12

Views: 6921

Answers (1)

Julian Pieles
Julian Pieles

Reputation: 4016

To answer my own question with the help of the comments:

URL resource = ClassLoader.getSystemResource("application.conf");
String configPath = URLDecoder.decode(resource.getFile(), "UTF-8");

Variable configPath then contains the correct path.

Upvotes: 19

Related Questions