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

S

stanislav.tomic

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.
 
A

Aaron Steed

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.
 
S

stanislav.tomic

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?
 
S

stanislav.tomic

For example:

|-MyJAR.jar (containing my classes)
|-lib (containing my libraries, for example databes driver)
|-properties (containing my properties-file)
 
A

Andrew Thompson

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
 
S

stanislav.tomic

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.
 
A

Andrew Thompson

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.
 
L

Lew

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.
 
A

Andrew Thompson

Lew said:
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
 
S

stanislav.tomic

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.
 
T

Thomas Hawtin

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
 
A

Andrew Thompson

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. said:
...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
 
R

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top