How can I write values in a properties-File which is located in a JAR without copying the whole JAR?

Discussion in 'Java' started by stanislav.tomic@gmail.com, Aug 17, 2007.

  1. Guest

    Hi people out there :)

    that's my first time in a diskussion-group. Let's see if it works. :)

    I have a question concerning properties-files in Java.

    Writing in a properties-file and reading out of the file is working as
    long as I don't put my files in a JAR.

    The adventureous part of the programm is that I'm trying to make an
    application which should be able to remember user-settings (write
    values in properties-file) and load them (read out values of
    properties-file) when started next time.

    I've tried different things, but I don't understand how to combine
    UPDATEABLE properties-files with JAR-archives? (How to put a
    properties-file in a JAR and write new values into it during
    runtime.) Reading is possible with different streams, but where is
    the sense of a properties-file if I can't add new data to it, while
    the application is running. Is there another way of saving properties
    which are necessary for the application? Of course the solution should
    only use relativ paths, to make sure that the user is able to access
    it.

    I would prefer to have my files in a JAR, cause it's easier to
    distribute. (The user just has to unzip a ZIP-archive, which includes
    a folder with the necessary libraries and a JAR-file with my classes.)

    So, that's the problem which I'm trying to solve in the last couple of
    days. Is anybody out ther, who can explain me how to solve that
    problem?

    Thanks in advance for your help,
    Stani.
    , Aug 17, 2007
    #1
    1. Advertising

  2. Aaron Steed Guest

    On Aug 17, 3:42 am, wrote:
    > Hi people out there :)
    >
    > that's my first time in a diskussion-group. Let's see if it works. :)
    >
    > I have a question concerning properties-files in Java.
    >
    > Writing in a properties-file and reading out of the file is working as
    > long as I don't put my files in a JAR.
    >
    > The adventureous part of the programm is that I'm trying to make an
    > application which should be able to remember user-settings (write
    > values in properties-file) and load them (read out values of
    > properties-file) when started next time.
    >
    > I've tried different things, but I don't understand how to combine
    > UPDATEABLE properties-files with JAR-archives? (How to put a
    > properties-file in a JAR and write new values into it during
    > runtime.) Reading is possible with different streams, but where is
    > the sense of a properties-file if I can't add new data to it, while
    > the application is running. Is there another way of saving properties
    > which are necessary for the application? Of course the solution should
    > only use relativ paths, to make sure that the user is able to access
    > it.
    >
    > I would prefer to have my files in a JAR, cause it's easier to
    > distribute. (The user just has to unzip a ZIP-archive, which includes
    > a folder with the necessary libraries and a JAR-file with my classes.)
    >
    > So, that's the problem which I'm trying to solve in the last couple of
    > days. Is anybody out ther, who can explain me how to solve that
    > problem?
    >
    > Thanks in advance for your help,
    > Stani.


    you can open it and edit it with the Winrar program i believe.
    Located at http://www.rarlabs.com/
    You should be able to open the jar file and edit its contents.
    Aaron Steed, Aug 17, 2007
    #2
    1. Advertising

  3. Guest

    Thank you for the quick reply Aaron.

    I've been thinking about using another application for updating my
    existing file too. But I would prefer to do it all in Java without an
    external application, because I want to be able to be independent of
    other programms.

    And then there is also the problem, that during runtime the JAR-
    archive won't be accessible.

    How do other people solve the problem with the properties-file? Should
    I save it in another folder and like my libraries and just access it
    with an relative path? Is that possible?
    , Aug 17, 2007
    #3
  4. Aaron Steed Guest

    On Aug 17, 3:50 am, Aaron Steed <> wrote:
    > On Aug 17, 3:42 am, wrote:
    >
    >
    >
    > > Hi people out there :)

    >
    > > that's my first time in a diskussion-group. Let's see if it works. :)

    >
    > > I have a question concerning properties-files in Java.

    >
    > > Writing in a properties-file and reading out of the file is working as
    > > long as I don't put my files in a JAR.

    >
    > > The adventureous part of the programm is that I'm trying to make an
    > > application which should be able to remember user-settings (write
    > > values in properties-file) and load them (read out values of
    > > properties-file) when started next time.

    >
    > > I've tried different things, but I don't understand how to combine
    > > UPDATEABLE properties-files with JAR-archives? (How to put a
    > > properties-file in a JAR and write new values into it during
    > > runtime.) Reading is possible with different streams, but where is
    > > the sense of a properties-file if I can't add new data to it, while
    > > the application is running. Is there another way of saving properties
    > > which are necessary for the application? Of course the solution should
    > > only use relativ paths, to make sure that the user is able to access
    > > it.

    >
    > > I would prefer to have my files in a JAR, cause it's easier to
    > > distribute. (The user just has to unzip a ZIP-archive, which includes
    > > a folder with the necessary libraries and a JAR-file with my classes.)

    >
    > > So, that's the problem which I'm trying to solve in the last couple of
    > > days. Is anybody out ther, who can explain me how to solve that
    > > problem?

    >
    > > Thanks in advance for your help,
    > > Stani.

    >
    > you can open it and edit it with the Winrar program i believe.
    > Located athttp://www.rarlabs.com/
    > You should be able to open the jar file and edit its contents.


    Im sorry, i had a misunderstanding. Disregard that last message.
    Aaron Steed, Aug 17, 2007
    #4
  5. Guest

    For example:

    |-MyJAR.jar (containing my classes)
    |-lib (containing my libraries, for example databes driver)
    |-properties (containing my properties-file)
    , Aug 17, 2007
    #5
  6. wrote:
    ...
    >I have a question concerning properties-files in Java.
    >
    >Writing in a properties-file and reading out of the file is working as
    >long as I don't put my files in a JAR.


    The best approach to user here, is to treat the
    properties file in the jar as simply an 'initial'
    properties file.

    Check in "user.home"/our/backward/domain/
    for the properties file. If it is not there, read it
    from out of the jar, and write it to there.

    Proceed loading the property file from the
    sub-directory of user.home.

    Using this scheme, you can also offer a
    'revert properties to defaults' option in the
    GUI, by simply retoring them from unchanged
    version in the Jar. ;-)

    But no, attempts to *edit* or *update* the actual
    resource in the Jar file will probably fail, or end
    in a very fragile application.

    >The adventureous part of the programm is that I'm trying to make an
    >application which should be able to remember user-settings (write
    >values in properties-file) and load them (read out values of
    >properties-file) when started next time.


    Here is an example of storing user preferences.
    <http://www.physci.org/jws/#ps>
    It makes use of the web start API's PersistenceService
    to do the same basic thing I described above, but for a
    sandboxed app. launched using web start (and with no
    specific '.properties' file to begin with).

    >...(The user just has to unzip a ZIP-archive, which includes
    >a folder with the necessary libraries and a JAR-file with my classes.)


    'Tut-tut' - use web start for the launch - much easier
    for the end-user.

    HTH

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
    Andrew Thompson, Aug 17, 2007
    #6
  7. Guest

    Thank you Andrew,

    I will try that out. That sounds good.

    Will other applications be able to access the properties-file in the
    subfolder? Is there a way of limiting the access to the properties-
    file only to my application?

    You are right. I'll use webstart in the second development version. In
    the first I'm forced to use an clientbased application.

    Thanks again,
    Stani.
    , Aug 17, 2007
    #7
  8. wrote:
    ...
    >You are right. I'll use webstart in the second development version. In
    >the first I'm forced to use an clientbased application.


    What does a 'clientbased application' mean where
    you come from? It means little to me.

    If you are speaking of a desk-top application with
    a GUI, then that is what web start was meant to
    launch. Did you actually try the launch link
    available at that web page anchor I put earlier?
    You can see it on-screen. Please try the
    launch link, and report your experiences.

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via http://www.javakb.com
    Andrew Thompson, Aug 17, 2007
    #8
  9. Lew Guest

    Re: How can I write values in a properties-File which is locatedin a JAR without copying the whole JAR?

    wrote:
    > Will other applications be able to access the properties-file in the
    > subfolder? Is there a way of limiting the access to the properties-
    > file only to my application?


    No.

    It's the customer's computer, not yours.

    --
    Lew
    Lew, Aug 17, 2007
    #9
  10. Lew wrote:
    > wrote:
    >> Will other applications be able to access the properties-file in the
    >> subfolder? Is there a way of limiting the access to the properties-
    >> file only to my application?

    >
    >No.
    >
    >It's the customer's computer, not yours.


    Actually, if using the web start PS, apps. are limited to
    'addresses' based upon the JNLP codebase*, so they can
    corrupt and mess up files for apps. from the same
    codebase, but no other ones**.

    <http://java.sun.com/javase/6/docs/jre/api/javaws/jnlp/javax/jnlp/PersistenceService.html
    >

    * "An application is only allowed to access data stored with a
    URL that is based on its codebase. For example, given the
    codebase http://www.mysite.com/apps/App1/, the application
    would be allowed to access the data at the associated URLs:

    http://www.mysite.com/apps/App1/
    http://www.mysite.com/apps/
    http://www.mysite.com/
    This scheme allows sharing of data between different applications
    from the same host. "

    The first method I outlined, using something that was
    supposed to represent the package name or similar of the
    main() class of the app., as the sub-directory, is a 'good bet'
    way to avoid name collisions with files written by other
    applications. Put them in the root of user.home, at your own
    risk, but nothing will save them from intentional sabotage.

    ** Of course, the user or their Sys. Admin., can do pretty
    much the heck what they want, including relocating the
    entire cache, or uninstalling parts, or all of an app.

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
    Andrew Thompson, Aug 17, 2007
    #10
  11. Guest

    Hi Andrew,

    Today after coming home from work, I tried for the first time to make
    a Java Web Start application. It's just a simple Hello-world-
    application for the beginning. But I'll try it later on with the big
    one I'm working at.

    I don't know a lot about JNLP and I'm not sure if it allows my
    application to do all the things like usual, for example database-
    connections, access directories for user, write files, and so on. Once
    the user gives his permission it should be okay, but I only wrote a
    simple programm with it and I don't have any experience how it works
    with bigger ones.

    But I see a lot of possibilities where I could use it. I'll just have
    to get more into it.

    I checked also your links. I did that when you posted them. Also
    checked your website.

    Thank you for your help Andrew. On the weekend I'll have more time to
    try things out.

    Greetings,
    Stani.
    , Aug 17, 2007
    #11
  12. Re: How can I write values in a properties-File which is locatedin a JAR without copying the whole JAR?

    wrote:
    >
    > I don't know a lot about JNLP and I'm not sure if it allows my
    > application to do all the things like usual, for example database-
    > connections, access directories for user, write files, and so on. Once
    > the user gives his permission it should be okay, but I only wrote a
    > simple programm with it and I don't have any experience how it works
    > with bigger ones.


    So long as your database driver is written in pure Java and uses sockets
    to connect to the same machine the JNLP application was downloaded from,
    you shouldn't have a problem.

    There are APIs (javax.jnlp) to access files and directories. This does
    however throw some pointless dialog boxes.

    Tom Hawtin
    Thomas Hawtin, Aug 18, 2007
    #12
  13. Thomas Hawtin wrote:
    >> I don't know a lot about JNLP and I'm not sure if it allows my
    >> application to do all the things like usual,


    While a singed, trusted, all-permissions app. can do
    anything a standard desk-top app. can do, the JNLP
    API allows even *sandboxed* apps. to do most of the
    more interesting things.

    >So long as your database driver is written in pure Java and uses sockets
    >to connect to the same machine the JNLP application was downloaded from,
    >you shouldn't have a problem.
    >
    >There are APIs (javax.jnlp) to access files and directories.


    E.G. <http://www.physci.org/jws/#fs>

    >...This does
    >however throw some pointless dialog boxes.


    Not if the app. is signed, requests extended privileges,
    and is trusted by the end-user. e.g.
    <http://www.physci.org/jws/filetest.jnlp>
    ( Oh, well - I suppose you might need to take my
    word for it - given your ..attitude to digitally signed
    apps. ;)

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
    Andrew Thompson, Aug 18, 2007
    #13
  14. Roedy Green Guest

    On Fri, 17 Aug 2007 01:42:56 -0700, wrote,
    quoted or indirectly quoted someone who said :

    >Writing in a properties-file and reading out of the file is working as
    >long as I don't put my files in a JAR.


    What you are trying to do does make sense to me. If the classes in
    the jar are updated and a new jar distributed, you will lose your
    jar-based property changes when it is installed.

    Jars are normally considered read-only. To write or update the Jar
    file, normally you use the jar.exe utility. Since jars are just zip
    files with extra members, you can also use ZIP utilities such as PKZIP
    and WinZip.

    You can also read and write the jar files from Java with the ZipEntry,
    ZipException, ZipFile, ZipInputStream and ZipOutputStream classes.
    While a jar is in use, some OS's may lock it, so don't count on being
    able to update jars on the fly when you are executing classes from
    them.

    Java Web Start makes it easy to find a spot to hide the Properties
    file.

    You might use the Preferences mechanism to store your property data.

    See http://mindprod.com/jgloss/javawebstart.html
    http://mindprod.com/jgloss/persistence.html
    http://mindprod.com/jgloss/preferences.html
    http://mindprod.com/jgloss/jar.html
    http://mindprod.com/jgloss/zip.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 18, 2007
    #14
    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. nobody
    Replies:
    1
    Views:
    524
    Venkatesh
    Apr 26, 2006
  2. \A_Michigan_User\
    Replies:
    2
    Views:
    873
    \A_Michigan_User\
    Aug 21, 2006
  3. Plasmafire
    Replies:
    1
    Views:
    7,480
    Thomas Fritsch
    Jun 19, 2007
  4. J. J. Ramsey
    Replies:
    3
    Views:
    729
  5. Hole
    Replies:
    5
    Views:
    1,435
    John B. Matthews
    Sep 23, 2009
Loading...

Share This Page