Java app launch dir?

Discussion in 'Java' started by Michael Brown, Jul 29, 2004.

  1. How can my standalone Java app discover what directory it lives in? I want
    to have the app look in that directory for a default properties file. I
    don't think this is necessarily what is returned by
    System.getProperty("user.dir"), is it? BTW, app is in a jar, so I'm looking
    for what directory the jar is in.

    Thanks....

    Mike
     
    Michael Brown, Jul 29, 2004
    #1
    1. Advertising

  2. Michael Brown

    KC Wong Guest

    > How can my standalone Java app discover what directory it lives in? I
    want
    > to have the app look in that directory for a default properties file. I
    > don't think this is necessarily what is returned by
    > System.getProperty("user.dir"), is it? BTW, app is in a jar, so I'm

    looking
    > for what directory the jar is in.


    If your properties file are in the same directory as the JAR, then you don't
    need to know where exactly it is...

    Take a look at the Java API docs, about java.lang.Class, methods
    getResource() and getResourceAsStream().

    e.g.

    private static final String PROGRAM_CONFIG = "/config.ini";

    class Foo {
    public Foo() {
    InputStream configStream =
    getClass().getResourceAsStream(PROGRAM_CONFIG);
    }
    }
     
    KC Wong, Jul 29, 2004
    #2
    1. Advertising

  3. Michael Brown

    Liz Guest

    "KC Wong" <> wrote in message
    news:...
    > > How can my standalone Java app discover what directory it lives in? I

    > want
    > > to have the app look in that directory for a default properties file. I
    > > don't think this is necessarily what is returned by
    > > System.getProperty("user.dir"), is it? BTW, app is in a jar, so I'm

    > looking
    > > for what directory the jar is in.

    >
    > If your properties file are in the same directory as the JAR, then you

    don't
    > need to know where exactly it is...
    >
    > Take a look at the Java API docs, about java.lang.Class, methods
    > getResource() and getResourceAsStream().
    >
    > e.g.
    >
    > private static final String PROGRAM_CONFIG = "/config.ini";
    >
    > class Foo {
    > public Foo() {
    > InputStream configStream =
    > getClass().getResourceAsStream(PROGRAM_CONFIG);
    > }
    > }
    >


    I do something like this
    File dir = new File(".");
    but that gives me the directory where the "java" command was issued
    not necessarily the same as where the jar file lives.
     
    Liz, Jul 29, 2004
    #3
  4. Michael Brown

    Alan Moore Guest

    On Wed, 28 Jul 2004 21:12:05 -0500, "Michael Brown" <>
    wrote:

    >How can my standalone Java app discover what directory it lives in? I want
    >to have the app look in that directory for a default properties file. I
    >don't think this is necessarily what is returned by
    >System.getProperty("user.dir"), is it? BTW, app is in a jar, so I'm looking
    >for what directory the jar is in.
    >
    >Thanks....
    >
    >Mike
    >

    String s = MyApp.class.getResource("MyApp.class").getFile();
    s = s.substring(5, s.indexOf("!"));
    File installDir = new File(s.substring(0, s.lastIndexOf("/")));
     
    Alan Moore, Jul 29, 2004
    #4
  5. Michael Brown

    Jacob Guest

    Michael Brown wrote:

    > How can my standalone Java app discover what directory it lives in? I want
    > to have the app look in that directory for a default properties file. I
    > don't think this is necessarily what is returned by
    > System.getProperty("user.dir"), is it? BTW, app is in a jar, so I'm looking
    > for what directory the jar is in.


    The common approach is to put the properties file
    *inside* the jar. This works fine for site specific
    deployment configuration etc. To ease the deployment
    process, you might put it into a separate jar file
    (app.jar + someCustomer.jar) which will be equivalent
    as seen from your application.

    This may however not be appropriate for all situations:
    User preferences is updated per session, and should
    live on the client machine. However, you have no
    knowledge of the directory of your jar file, and it
    is a bad idea to look there anyway. Using "user.dir"
    is one option, but using the Preferences API from the
    JDK is a lot better.
     
    Jacob, Jul 29, 2004
    #5
    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. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    438
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. Replies:
    3
    Views:
    536
    Daniel Pitts
    Jan 3, 2007
  3. Yao Qi
    Replies:
    30
    Views:
    1,392
    Roedy Green
    Jul 4, 2007
  4. Replies:
    6
    Views:
    555
  5. Matthew Denner
    Replies:
    1
    Views:
    197
Loading...

Share This Page