finding the directory a .jar file was launched from

S

steve

Hi,

what is the best way via code to find the directory a jar file was launched
from .


ourProgramDirectory = System.getProperty("user.dir"); // this is not
correct!!!!!!


so far im looking at :
classURL = bootLoader.class.getResource("");

then cutting off the last directory path.

Steve
 
O

Owen Jacobson

Hi,

what is the best way via code to find the directory a jar file was
launched from .


ourProgramDirectory = System.getProperty("user.dir"); // this is not
correct!!!!!!

How is it not correct? What does it return, and what are you expecting it
to return?

user.dir will contain the current working directory of the program (the
directory from which relative paths will be resolved), which is 90% of the
time what you really want. If this is _not_ what you need, explain
clearly what you need and why.

Owen
 
L

Lucy

steve said:
Hi,

what is the best way via code to find the directory a jar file was launched
from .


ourProgramDirectory = System.getProperty("user.dir"); // this is not
correct!!!!!!


so far im looking at :
classURL = bootLoader.class.getResource("");

then cutting off the last directory path.

Steve
Is it an Applet?
 
S

steve

How is it not correct? What does it return, and what are you expecting it
to return?

user.dir will contain the current working directory of the program (the
directory from which relative paths will be resolved), which is 90% of the
time what you really want. If this is _not_ what you need, explain
clearly what you need and why.

Owen

basically i was using user directory, but then i ran
appperfect java profiler on the app.

lo & behold, the working directory changed!!, I would guess because the
profiler is setting its own user directory & launching my app from inside the
profiler
which completely buggers up the running app, because it assumes it's files
are in the user directory.
 
T

Thomas Kellerer

steve wrote on 11.06.2005 01:58:
Hi,

what is the best way via code to find the directory a jar file was launched
from .


ourProgramDirectory = System.getProperty("user.dir"); // this is not
correct!!!!!!


so far im looking at :
classURL = bootLoader.class.getResource("");

then cutting off the last directory path.

Steve

Try this:

CodeSource source = YourClass.class.getProtectionDomain().getCodeSource();

File installDir;

try
{
URI sourceURI = new URI(source.getLocation().toString());
installDir = new File(sourceURI);
}
catch (URISyntaxException e)
{
return null;
}
catch (IllegalArgumentException e)
{
return null;
}

return installDir.getAbsolutePath();

Where "YourClass" is the class where you implement this.

Thomas
 
O

Owen Jacobson

basically i was using user directory, but then i ran appperfect java
profiler on the app.

lo & behold, the working directory changed!!, I would guess because the
profiler is setting its own user directory & launching my app from inside
the profiler
which completely buggers up the running app, because it assumes it's files
are in the user directory.

This is a perfectly valid assumption for your application to make. The
problem you actually want to solve is "how do I make my profiler use the
correct working directory?" This is probably somewhere in the AppPerfect
project settings; you may want to ask them or read the documentation for
more clues.

Owen
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top