Assume standard maven setup.
Say in your resources folder you have a file abc
.
In Java, how can I get absolute path to the file please?
Assume standard maven setup.
Say in your resources folder you have a file abc
.
In Java, how can I get absolute path to the file please?
To return a file or filepath
The proper way that actually works:
It doesn't matter now where the file in the classpath physically is, it will be found as long as the resource is actually a file and not a JAR entry.
(The seemingly obvious
new File(resource.getPath())
doesn't work for all paths! The path is still URL-encoded!)You can create a
File
object and usegetAbsolutePath
method:Create the classLoader instance of the class you need, then you can access the files or resources easily. now you access path using
getPath()
method of that class.You must specifie path started from
/