Joost said:
I want to get the startup directory of my Java program to build a
relative path to some local resources. How can I do that?
Before directly answering your question, note that you should use the
Class.getResource and Class.getResourceAsStream (or the corresponding
methods in ClassLoader) to find associated resources located with the
class file. This allows your code to be distributed using a number of
methods and acquire the resources using the same code.
Now, did you mean the ambient "current directory" property of many
operating systems or the directory in the classpath under which your
main class was found?
The former can be found via
System.getProperty("user.dir")
and I've acquired the latter through
MainClass.class
.getProtectionDomain()
.getCodeSource()
.getLocation()
I've this last one to acquire resources that were shipped alongside a
JAR file rather than in it, but normally I just put the resources in the
class hierarchy next to the class that needs it.