Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
stanislav.tomic@gmail.com - 17 Aug 2007 09:42 GMT
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.
Aaron Steed - 17 Aug 2007 09:50 GMT
On Aug 17, 3:42 am, stanislav.to...@gmail.com wrote:
> Hi people out there :)
>
[quoted text clipped - 30 lines]
> 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.
stanislav.tomic@gmail.com - 17 Aug 2007 10:03 GMT
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?
stanislav.tomic@gmail.com - 17 Aug 2007 10:06 GMT
For example:

|-MyJAR.jar (containing my classes)
|-lib (containing my libraries, for example databes driver)
|-properties (containing my properties-file)
Aaron Steed - 17 Aug 2007 10:05 GMT
> On Aug 17, 3:42 am, stanislav.to...@gmail.com wrote:
>
[quoted text clipped - 36 lines]
> 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.
Andrew Thompson - 17 Aug 2007 10:11 GMT
...
>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

Signature

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

stanislav.tomic@gmail.com - 17 Aug 2007 10:37 GMT
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.
Andrew Thompson - 17 Aug 2007 11:43 GMT
...
>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.

Signature

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

Lew - 17 Aug 2007 14:10 GMT
stanislav.tomic@gmail.com 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.

Signature

Lew

Andrew Thompson - 17 Aug 2007 14:38 GMT
>stanislav.tomic@gmail.com wrote:
>> Will other applications be able to access the properties-file in the
[quoted text clipped - 4 lines]
>
>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/PersistenceServ
ice.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.

Signature

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

stanislav.tomic@gmail.com - 17 Aug 2007 23:42 GMT
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.
Thomas Hawtin - 17 Aug 2007 23:54 GMT
> 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
Andrew Thompson - 18 Aug 2007 02:52 GMT
>> 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. ;)

Signature

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

Roedy Green - 18 Aug 2007 21:41 GMT
>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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.