Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > finding the directory a .jar file was launched from

Reply
Thread Tools

finding the directory a .jar file was launched from

 
 
steve
Guest
Posts: n/a
 
      06-10-2005
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


 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      06-11-2005
On Sat, 11 Jun 2005 07:58:02 +0800, steve wrote:

> 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
 
Reply With Quote
 
 
 
 
Lucy
Guest
Posts: n/a
 
      06-11-2005

"steve" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). com...
> 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?


 
Reply With Quote
 
steve
Guest
Posts: n/a
 
      06-11-2005
On Sat, 11 Jun 2005 10:40:23 +0800, Owen Jacobson wrote
(in article <(E-Mail Removed)>):

> On Sat, 11 Jun 2005 07:58:02 +0800, steve wrote:
>
>> 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


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.



 
Reply With Quote
 
Thomas Kellerer
Guest
Posts: n/a
 
      06-11-2005
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().getCodeSourc e();

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
 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      06-13-2005
On Sat, 11 Jun 2005 15:14:06 +0800, steve wrote:

> Owen Jacobson wrote:
>> steve wrote:
>>> 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?


> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the local directory of a file in the python path are Dogue Python 1 05-18-2011 01:53 AM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Finding directory in file list. mihirtr@gmail.com Perl Misc 2 12-27-2006 08:04 AM
finding the last file created in a directory Petr Man Python 0 06-30-2006 12:43 PM
Problem finding directory ** File dir = new File(URL+aFile_to_add); vivienne wykes Java 1 08-03-2004 04:12 PM



Advertisments