How would I convert the name of a file on the classpath to a real filename?
For example, let's say the directory "C:\workspace\project\target\classes"
is on your classpath. Within that directory is a file, such as info.properties
.
How would you determine (at runtime) the absolute file path to the info.properties file, given only the string "info.properties"
?
The result would be something like "C:\workspace\project\target\classes\info.properties"
.
Why is this useful? When writing unit tests, you may want to access files bundled in your test resources (src/main/resources
) but are working with a third-party library or other system that requires a true filename, not a relative classpath reference.
Note: I've answered this question myself, as I feel it's a useful trick, but it looks like no one has ever asked this question before.