10

Is there a way for java program to determine its location in the file system?

3 Answers 3

13

You can use CodeSource#getLocation() for this. The CodeSource is available by ProtectionDomain#getCodeSource(). The ProtectionDomain in turn is available by Class#getProtectionDomain().

URL location = getClass().getProtectionDomain().getCodeSource().getLocation();
File file = new File(location.getPath());
// ...

This returns the exact location of the Class in 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.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("filename.ext");
File file = new File(resource.getPath());
// ...

You can even get it as an InputStream using ClassLoader#getResourceAsStream().

InputStream input = classLoader.getResourceAsStream("filename.ext");
// ...

That's also the normal way of using packaged resources. If it's located inside a package, then use for example com/example/filename.ext instead.

Sign up to request clarification or add additional context in comments.

8 Comments

+1, but note that ProtectionDomain.getCodeSource() and CodeSource.getLocation() can both be null.
@bkail: It may indeed return null when it's loaded using a poorly designed homegrown classloader, or when it's loaded from some external stream source, etc. This however usually doesn't apply on "concrete" Java programs with a main().
This definitely does do the job - but it doesn't look very nice. What I need to do is load a configuration file which needs to be in the same folder as my jar but can be run from any location. Is this the right way to get the file location info?
So it's already in the classpath? Then just use the classloader. I updated my answer.
getResourceAsStream suggestion is a good one. FYI, getResource will return "jar:file:path!/resource", so you'll need to deconstruct that before passing to File. Even if you assume a directory-based classpath, URL.getPath() from getResource will be URL-encoded (e.g., "Program%20Files"), so you need to decode (or just convert the URL to a URI) before converting to File. (Sorry for the pedantry; I worked in class loader support for three years and got to see/experience the pain of all these nuances :-)).
|
0

For me this worked, when I knew what was the exact name of the file:

File f = new File("OutFile.txt"); System.out.println("f.getAbsolutePath() = " + f.getAbsolutePath());

Or there is this solution too: http://docs.oracle.com/javase/tutorial/essential/io/find.html

Comments

-1

if you want to get the "working directory" for the currently running program, then just use:

new File("");

1 Comment

The working directory is not necessarily where the program is located.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.