how to get application path? Is that my method in getting path wrong?

Discussion in 'Java' started by James, Dec 7, 2004.

  1. James

    James Guest

    eg.
    c:\myapp\test.class
    c:\myapp\icon\icon1.gif
    c:\myapp\setting.cfg

    when I compile and run test.class
    in coding, I write
    something.setImageIcon(getClass().getResource("/icon1.gif"));
    after process something, the program will save the file setting in
    setting.cfg
    something.savefile(new
    File(getClass().getResource("/setting.cfg").toString()));

    all of this run ok, the image got load up, then when save setting, it got
    write back into setting.cfg file.

    Later I group all of this in 1 jar file.
    then when I try the program, it cannot run anymore. something related to
    nullpointerexception.

    I try to get out the path and I found out that when running in normal
    class(without jar), that code can get correct path and the file.
    but when run in jar(with main class) that code can't get correct path.
    It got something at between the parent path and class path.
    at center it come out something like jar file(it act like 1 level path)

    so I want to ask you all, normally how do you all get the running path
    correctly so that can load the image and save file back to running class's
    path
     
    James, Dec 7, 2004
    #1
    1. Advertising

  2. James

    Guest

    James wrote:
    > eg.
    > c:\myapp\test.class
    > c:\myapp\icon\icon1.gif
    > c:\myapp\setting.cfg
    >
    > when I compile and run test.class
    > in coding, I write
    > something.setImageIcon(getClass().getResource("/icon1.gif"));
    > after process something, the program will save the file setting in
    > setting.cfg
    > something.savefile(new
    > File(getClass().getResource("/setting.cfg").toString()));
    >
    > all of this run ok, the image got load up, then when save setting, it

    got
    > write back into setting.cfg file.
    >
    > Later I group all of this in 1 jar file.
    > then when I try the program, it cannot run anymore. something related

    to
    > nullpointerexception.


    Any specific 'something', or are you inviting us to guess what your
    actual error was?


    > I try to get out the path and I found out that when running in normal
    > class(without jar), that code can get correct path and the file.
    > but when run in jar(with main class) that code can't get correct

    path.
    > It got something at between the parent path and class path.
    > at center it come out something like jar file(it act like 1 level

    path)

    Examples?


    > so I want to ask you all, normally how do you all get the running

    path
    > correctly so that can load the image and save file back to running

    class's
    > path


    When working with getResource() you aren't really working with regular
    files on a disk, like you would with a java.io.File object, but data
    which acts as resources to classes. Physically these can be stored in
    many ways - just as class files themselves can be stored in many ways:
    as files on the file system, as entries in a Jar, even as records in
    a database - so long as there is a classloader which knowns how to
    fetch them from a given identifier String.

    If you want to read and write to a local disk, use java.io.File
    and an appropriate chain of Input/Output and Reader/Writer streams.
    Don't use getResource() , as when your representation changes (like
    you go from packages mapped out on the local disk, to packed in
    a Jar file) then things are likely to break.


    -FISH- ><>
     
    , Dec 7, 2004
    #2
    1. Advertising

  3. On Tue, 7 Dec 2004 17:39:26 +0800, James wrote:

    > eg.
    > c:\myapp\test.class


    is 'test.class' in 'myapp' package, or is it in the default package?

    > c:\myapp\icon\icon1.gif
    > c:\myapp\setting.cfg
    >
    > when I compile and run test.class
    > in coding, I write
    > something.setImageIcon(getClass().getResource("/icon1.gif"));


    If 'default'. The path to the icon is..

    URL url = getClass().getResource("/icon/icon1.gif");
    // now TEST the URL!
    System.out.println( "icon1 URL: " + url );
    something.setImageIcon( url );

    > after process something, the program will save the file setting in
    > setting.cfg
    > something.savefile(new
    > File(getClass().getResource("/setting.cfg").toString()));


    You cannot save the data back into the class jar, and it it
    best *not* to write it in the progtam path but the user's
    'user.home' directory..
    <http://www.physci.org/codes/javafaq.jsp#path>

    > all of this run ok, the image got load up, then when save setting, it got
    > write back into setting.cfg file.
    >
    > Later I group all of this in 1 jar file.
    > then when I try the program, it cannot run anymore. something related to
    > nullpointerexception.


    To expand on what FISH wrote..
    <http://www.physci.org/codes/javafaq.jsp#exact>

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Dec 7, 2004
    #3
    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. Water Cooler v2

    Getting the application path

    Water Cooler v2, Sep 24, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    11,443
    Juan T. Llibre
    Sep 24, 2005
  2. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    Getting Application Path Physical location

    =?Utf-8?B?UmFlZCBTYXdhbGhh?=, Oct 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,909
    Mark Monster
    Oct 3, 2005
  3. Mitko Haralanov
    Replies:
    17
    Views:
    434
    Ben Finney
    Oct 20, 2006
  4. haiwen
    Replies:
    1
    Views:
    393
    haiwen
    Jan 28, 2004
  5. Hunt Jon
    Replies:
    2
    Views:
    125
    Phlip
    Dec 18, 2008
Loading...

Share This Page