Hi,
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?
TIA
Joost
Alan Krueger - 26 Nov 2005 15:43 GMT
> 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.
Joost Kraaijeveld - 26 Nov 2005 16:30 GMT
Hi Alan,
> The former can be found via
>
[quoted text clipped - 10 lines]
> JAR file rather than in it, but normally I just put the resources in the
> class hierarchy next to the class that needs it.
Thanks, both answers will work for me.
Joost
Roedy Green - 26 Nov 2005 16:31 GMT
On Sat, 26 Nov 2005 11:49:28 +0100, Joost Kraaijeveld
<J.Kraaijeveld@Askesis.nl> wrote, quoted or indirectly quoted someone
who 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?
You have to set up the classpath before your JVM starts. You can't
change it dynamically. However, you can write a custom classloader
that uses its own classpath. This is easier than you think.
See http://mindprod.com/jgloss/classloader.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Ike - 26 Nov 2005 21:45 GMT
If you;re talking about windows, here is what I use:
....= (new LocalDir()).getLocalDirRef();
//Ike
/*
* localDir
*
* Utility class to get String and File reference to the local directory
* that the class is executing in.
*/
import java.io.File;
/**
*
* @author Mark Kozel
* @version 1.4.1
* @since Created on December 16, 2002
*/
public class LocalDir
{
public LocalDir()
{
}
/**
* Returns the disk file name of the class that is executing.
*
* @param none
* @return Name of class that is currently executing
*/
public String getClassName()
{
String thisClassName;
//Build a string with executing class's name
thisClassName = this.getClass().getName();
thisClassName = thisClassName.substring(thisClassName.lastIndexOf(".")
+ 1,thisClassName.length());
thisClassName += ".class"; //this is the name of the bytecode file
that is executing
return thisClassName;
}
/**
* Returns the name of the local directory based on the results of a call
to getClassName()
*
* @param none
* @return Name of directory that contains the executing class
*/
public String getLocalDirName()
{
String localDirName;
//Use that name to get a URL to the directory we are executing in
java.net.URL myURL = this.getClass().getResource(getClassName());
//Open a URL to the our .class file
//Clean up the URL and make a String with absolute path name
localDirName = myURL.getPath(); //Strip path to URL object out
localDirName = myURL.getPath().replaceAll("%20", " "); //change %20
chars to spaces
//Get the current execution directory
localDirName =
localDirName.substring(0,localDirName.lastIndexOf("/")); //clean off the
file name
return localDirName;
}
/**
* Returns a File reference to the local directory based on the results
of a call to getClassName()
*
* @param none
* @return File object that points to the local directory
*/
public java.io.File getLocalDirRef()
{
File myFileObj;
//Make the file object and return it
myFileObj = new File(getLocalDirName());
return myFileObj;
}
}