How to find the startup directory of my program?

J

Joost Kraaijeveld

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
 
A

Alan Krueger

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.
 
J

Joost Kraaijeveld

Hi Alan,

Alan said:
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.
Thanks, both answers will work for me.

Joost
 
R

Roedy Green

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
 
I

Ike

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;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top