Is there a way for java program to determine its location in the file system?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
CodeSource#getLocation()for this. TheCodeSourceis available byProtectionDomain#getCodeSource(). TheProtectionDomainin turn is available byClass#getProtectionDomain().This returns the exact location of the
Classin question.Update: as per the comments, it’s apparently already in the classpath. You can then just use
ClassLoader#getResource()wherein you pass the root-package-relative path.You can even get it as an
InputStreamusingClassLoader#getResourceAsStream().That’s also the normal way of using packaged resources. If it’s located inside a package, then use for example
com/example/filename.extinstead.