vendredi 27 mai 2016

How to get the absolute path for the resources directory in Java?

I'm trying to find a way to get the absolute path for the resources folder in my junit test.

My doubt is not get a file in the resources folder (I now how to do this) but get the path to the resources folder.

Per example, if my file in my resources folder have this structure:

/opt/project/view/api/target/classes/
/opt/project/view/api/target/classes/file_in_resources.txt

I would like to get the location /opt/project/view/api/target/classes/ without use something ugly like:

URL location = this.getClass().getResource("/file_in_resources.txt");
String path = location.getPath();
String rightPath = path.substring(0, path.lastIndexOf("/"));

The idea is have a method like this.getClass().getResourcesPath(), but this kind of method not exists.

How to do?

Aucun commentaire:

Enregistrer un commentaire