Application Path

Discussion in 'Java' started by Roman, Dec 17, 2005.

  1. Roman

    Roman Guest

    Hello,

    could someone pleas tell me, how I find out from inside the program, in
    what path it is installed?

    The property user.dir returns only the working directory but this is not
    necessary the path to the Application.

    System.getenv(String) is deprecated.

    Thank you for any answers in advance.

    --
    Gruesse Roman
     
    Roman, Dec 17, 2005
    #1
    1. Advertising

  2. Try:
    System.getProperty("java.home");
    (then you could append bin/java -or- bin\java.exe -or- bin\javaw.exe)

    Regards
     
    Jean-Francois Briere, Dec 17, 2005
    #2
    1. Advertising

  3. Roman wrote:

    > could someone pleas tell me, how I find out from inside the program, in
    > what path it is installed?


    <http://www.physci.org/codes/javafaq.jsp#path>

    --
    Andrew Thompson
    physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
     
    Andrew Thompson, Dec 17, 2005
    #3
  4. Roman

    Roedy Green Guest

    On Sat, 17 Dec 2005 14:53:04 +0000, Roman <> wrote, quoted
    or indirectly quoted someone who said :

    >could someone pleas tell me, how I find out from inside the program, in
    >what path it is installed?


    There are several different meanings to that question. One of them is
    where did a given class file come from. See
    http://mindprod.com/jgloss/where.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 18, 2005
    #4
  5. Roman

    Roman Guest

    Thanks to all who answered. I got it.

    --
    Gruesse Roman
     
    Roman, Dec 18, 2005
    #5
  6. Roman

    David Segall Guest

    Roman <> wrote:

    >Thanks to all who answered. I got it.

    Could you post the code please?
     
    David Segall, Dec 18, 2005
    #6
  7. Roman

    Roman Guest

    David Segall wrote:
    > Roman <> wrote:
    >
    >
    >>Thanks to all who answered. I got it.

    >
    > Could you post the code please?


    Here a stand-alone example:


    import java.net.URL;

    public class App {
    public static void main(String[] args) {
    App app = new App();
    System.out.println(
    (app.getClass().getResource("App.class")).getPath()
    );
    }
    }


    --
    Gruesse Roman
     
    Roman, Dec 18, 2005
    #7
  8. Roman

    Roman Guest

    Roman wrote:
    > David Segall wrote:
    >
    >> Roman <> wrote:
    >>
    >>
    >>> Thanks to all who answered. I got it.

    >>
    >>
    >> Could you post the code please?

    >
    >
    > Here a stand-alone example:
    >
    >
    > import java.net.URL;
    >
    > public class App {
    > public static void main(String[] args) {
    > App app = new App();
    > System.out.println(
    > (app.getClass().getResource("App.class")).getPath()
    > );
    > }
    > }
    >
    >


    Of course you could replace "App.class" by

    app.getClass().getName() + ".class"

    --
    Gruesse Roman
     
    Roman, Dec 18, 2005
    #8
  9. Roman

    Chris Smith Guest

    Roman <> wrote:
    > Here a stand-alone example:
    >
    >
    > import java.net.URL;
    >
    > public class App {
    > public static void main(String[] args) {
    > App app = new App();
    > System.out.println(
    > (app.getClass().getResource("App.class")).getPath()
    > );
    > }
    > }


    This code doesn't work. Or rather, it only works if your application is
    composed of loose class files, but obviously isn't (or, at least,
    hopefully isn't!) the case when you deploy the application.

    I wrote a very complete and detailed answer to this question some time
    ago. It's temporarily available in Word format at the following rather
    ugly URL:

    > http://www.javamoderator.org/Java W...aneous/Finding Application Intrinsic Data.doc


    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 18, 2005
    #9
  10. Roman

    Guest

    , Dec 19, 2005
    #10
  11. Roman

    Roedy Green Guest

    Roedy Green, Dec 19, 2005
    #11
  12. Roman

    Roedy Green Guest

    On 18 Dec 2005 17:03:26 -0800, ""
    <> wrote, quoted or indirectly quoted someone
    who said :

    >No, System.getenv(String) is not deprecated.


    It was deprecated for a while, then made respectable again.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 19, 2005
    #12
  13. Roman

    Chris Smith Guest

    Roedy Green <> wrote:
    > IF you want I could format that as HTML and with an attribution add it
    > as http://mindprod.com/jgloss/intrinsicdata.html


    In this case I have to say no. I wrote that on my employer's time, and
    our plan was to collect a lot of similar documents, convert them to PDF
    in a similar format, and place them on MindIQ's web site to lure Java
    programmers' search engines in hopes that they'd see our commercial
    training courses. That plan isn't actually progressing, but I can't
    just give away content that technically belongs to MindIQ.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 19, 2005
    #13
  14. Roman

    David Segall Guest

    Roman <> wrote:

    >David Segall wrote:
    >> Roman <> wrote:
    >>
    >>
    >>>Thanks to all who answered. I got it.

    >>
    >> Could you post the code please?

    >
    >Here a stand-alone example:
    >
    >
    >import java.net.URL;
    >
    >public class App {
    > public static void main(String[] args) {
    > App app = new App();
    > System.out.println(
    > (app.getClass().getResource("App.class")).getPath()
    > );
    > }
    >}
    >
    >

    Thanks Roman. On my system that returns
    "file:/C:/Program%20Files/App/App.jar!/App.class". I was hoping for
    something that needed less post-processing to yield
    "C:program Files/App".
     
    David Segall, Dec 19, 2005
    #14
  15. >> app.getClass().getResource("App.class")

    > This code doesn't work. Or rather, it only works if your application is
    > composed of loose class files


    Actually the code works for either loose class files or class files
    within jar files.

    Regards
     
    Jean-Francois Briere, Dec 19, 2005
    #15
  16. Roman

    David Segall Guest

    Chris Smith <> wrote:

    >Roman <> wrote:
    >> Here a stand-alone example:
    >>
    >>
    >> import java.net.URL;
    >>
    >> public class App {
    >> public static void main(String[] args) {
    >> App app = new App();
    >> System.out.println(
    >> (app.getClass().getResource("App.class")).getPath()
    >> );
    >> }
    >> }

    >
    >This code doesn't work. Or rather, it only works if your application is
    >composed of loose class files, but obviously isn't (or, at least,
    >hopefully isn't!) the case when you deploy the application.
    >I wrote a very complete and detailed answer to this question some time
    >ago. It's temporarily available in Word format at the following rather
    >ugly URL:
    >
    >> http://www.javamoderator.org/Java W...aneous/Finding Application Intrinsic Data.doc

    Thank you for the code in technique #5 which returns the appropriately
    formatted directory name that I would expect.

    Could you explain why you say using .getResource() does not work? The
    raw result seems similar to that obtained from
    getProtectionDomain().getCodeSource() and I don't see why some similar
    post-processing would not yield the same directory name.
     
    David Segall, Dec 19, 2005
    #16
  17. Roman

    Chris Smith Guest

    David Segall <> wrote:
    > Could you explain why you say using .getResource() does not work? The
    > raw result seems similar to that obtained from
    > getProtectionDomain().getCodeSource() and I don't see why some similar
    > post-processing would not yield the same directory name.


    Well, for one thing, getResource() is not guaranteed to work on a class
    file in the first place. But in practice, it does work; so that's not
    the most important problem.

    The real problem is that the original code just doesn't work for code
    that's packaged in a JAR file. getResource() returns a URL with the
    "jar" scheme, and calling "getPath" on that URL doesn't do what you
    want. getCodeSource(), on the other hand, will return a file-scheme URL
    pointing to the JAR file itself. That is what you want.

    getCodeSource() is also more convenient even for loose classes, if they
    are in packages. getResource() returns a path that points deep into the
    package structure. getCodeSource() points to the root of that
    structure, which is almost certainly what you wanted in the first place.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 19, 2005
    #17
  18. Roman

    David Segall Guest

    Chris Smith <> wrote:

    >David Segall <> wrote:
    >> Could you explain why you say using .getResource() does not work? The
    >> raw result seems similar to that obtained from
    >> getProtectionDomain().getCodeSource() and I don't see why some similar
    >> post-processing would not yield the same directory name.

    >
    >Well, for one thing, getResource() is not guaranteed to work on a class
    >file in the first place. But in practice, it does work; so that's not
    >the most important problem.
    >
    >The real problem is that the original code just doesn't work for code
    >that's packaged in a JAR file. getResource() returns a URL with the
    >"jar" scheme, and calling "getPath" on that URL doesn't do what you
    >want. getCodeSource(), on the other hand, will return a file-scheme URL
    >pointing to the JAR file itself. That is what you want.
    >
    >getCodeSource() is also more convenient even for loose classes, if they
    >are in packages. getResource() returns a path that points deep into the
    >package structure. getCodeSource() points to the root of that
    >structure, which is almost certainly what you wanted in the first place.

    Thanks Chris. I'm convinced. I think it would be worthwhile to add
    this explanation to your essay since getResource() is most frequently
    proposed as the solution to the problem.
     
    David Segall, Dec 19, 2005
    #18
    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. Keith-Earl
    Replies:
    1
    Views:
    2,024
    Peter O'Reilly
    May 3, 2004
  2. Replies:
    0
    Views:
    2,290
  3. Mupota Muchelemba
    Replies:
    1
    Views:
    949
    Tony Morris
    Feb 4, 2004
  4. James
    Replies:
    2
    Views:
    13,954
    Andrew Thompson
    Dec 7, 2004
  5. haiwen
    Replies:
    1
    Views:
    396
    haiwen
    Jan 28, 2004
Loading...

Share This Page