finding the directory a .jar file was launched from

Discussion in 'Java' started by steve, Jun 11, 2005.

  1. steve

    steve Guest

    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
    steve, Jun 11, 2005
    #1
    1. Advertising

  2. 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
    Owen Jacobson, Jun 11, 2005
    #2
    1. Advertising

  3. steve

    Lucy Guest

    "steve" <> wrote in message
    news:...
    > 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?
    Lucy, Jun 11, 2005
    #3
  4. steve

    steve Guest

    On Sat, 11 Jun 2005 10:40:23 +0800, Owen Jacobson wrote
    (in article <>):

    > 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.
    steve, Jun 11, 2005
    #4
  5. 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
    Thomas Kellerer, Jun 11, 2005
    #5
  6. 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
    Owen Jacobson, Jun 13, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnold Peters
    Replies:
    0
    Views:
    565
    Arnold Peters
    Jan 5, 2005
  2. muttley
    Replies:
    0
    Views:
    2,715
    muttley
    Oct 20, 2005
  3. cyberco
    Replies:
    4
    Views:
    3,748
    Roedy Green
    Feb 14, 2006
  4. Karsten Wutzke
    Replies:
    0
    Views:
    633
    Karsten Wutzke
    Jun 27, 2007
  5. Qu0ll
    Replies:
    8
    Views:
    811
    John B. Matthews
    Aug 12, 2009
Loading...

Share This Page