getting jar file's place

Discussion in 'Java' started by Gergely Buday, May 27, 2004.

  1. Hi Folks,

    I know this is a recurring theme on java groups, but I do ask it
    again:

    I'd like to get the directory where my .jar file resides. I do know
    that it is not always possible, but my users will use the "W"
    operating system, and don't download my code from the net, so I expect
    my jar file to be in a simple directory.

    I have a directory structure

    ..
    demo.bat
    demo.jar
    img/ -- images
    dll/ -- dlls
    data/ -- database

    so I didn't put everything in the jar file, and I would not like to do
    so.

    How can I get the full path for "." ?

    - Gergely
    Gergely Buday, May 27, 2004
    #1
    1. Advertising

  2. (Gergely Buday) wrote:
    > I'd like to get the directory where my .jar file resides. I do know
    > that it is not always possible, but my users will use the "W"
    > operating system, and don't download my code from the net, so I expect
    > my jar file to be in a simple directory.


    Depending on what you want to do with that directory,

    static InputStream java.lang.ClassLoader.getSystemResourceAsStream(String name)

    might get you somewhere. The "resource" can be just about
    any file anywhere on the class path. As such it is actually
    quite similar to a 'new FileInputStream("dada")'.

    Harald.
    Harald Kirsch, May 27, 2004
    #2
    1. Advertising

  3. Gergely Buday

    Oscar kind Guest

    Gergely Buday <> wrote:
    > I know this is a recurring theme on java groups, but I do ask it
    > again:
    >
    > I'd like to get the directory where my .jar file resides. I do know
    > that it is not always possible, but my users will use the "W"
    > operating system, and don't download my code from the net, so I expect
    > my jar file to be in a simple directory.


    You can get the current directory using new File(".").getCanonicalPath(),
    and the classpath using System.getProperty("java.class.path") .
    Using this information, you can search your classpath for the jar file of
    your application.

    You can also make your life easier by creating an application jar file,
    optionally with "Class-Path" headers in the manifest. When run using
    "java -jar jarFile.jar" or "java.exe -jar jarFile.jar", this jar file will
    be the onbly entry on the classpath. Note that this is the default on
    Windows when the user double-clicks (a shortcut to) the jar file.


    > I have a directory structure

    [...]
    > so I didn't put everything in the jar file, and I would not like to do
    > so.
    >
    > How can I get the full path for "." ?


    Ok, I wrote too much. This'll do:
    String currentDir = new File(".").getCanonicalPath();


    Oscar

    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, May 27, 2004
    #3
  4. > I'd like to get the directory where my .jar file resides. I do know
    > that it is not always possible, but my users will use the "W"
    > operating system, and don't download my code from the net, so I expect
    > my jar file to be in a simple directory.

    <snip>
    > How can I get the full path for "." ?


    Something like this?
    <http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&selm=MPG.16a0e84858bc6961989bf3%40news.altopia.com>

    HTH,
    Michiel
    Michiel Konstapel, May 31, 2004
    #4
    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. lucy
    Replies:
    6
    Views:
    30,561
    steve
    Sep 7, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    554
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,688
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,725
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    638
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page