Get the directory where a Class (its Package) is stored

From Cuis CookBook
Jump to navigation Jump to search

Problem. You need to access some data file. You know it is a bad idea to put an absolute path to say where it is, tomorrow you may move your project in another computer and all those kind of absolute references would be broken. Then, you may know that, for exaple, your data files live in a directory nearby the package that uses them! So, just find where your class/package is and move from there.

Solution. I want to find in which directory the package containing the class Foobar lives, i do like this:

Foobar package packageDirectoryName.