Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

edit properties file inside a jar 1

Status
Not open for further replies.

samasamajam

Programmer
Dec 27, 2002
13
0
0
GB
Hi,
My standalone application is bundled into a jar file and uses a properties file for configuration settings and the properties file is also bundled inside the same jar.
For reading from the properties file,I use the following code snippet - this.getClass().getResourceAsStream(sPropertiesFile).
I want to create a functionality for editing the properties file within the same standalone application without having to manually unjar and update properties file. But i am not able to access the properties file. Is it possible to update the properties file that is inside the jar using classes which reside in the same jar.
 
I don't see why not... A jar is just a zip file, and you should be able to use a compression stream to edit it...
 
The [tt]java.util.zip[/tt] and [tt]java.util.jar[/tt] libs don't allow for in-place editing of Jar contents.
I recently needed to programmatically update the contents of a Jar. I achieved it by creating a new temporary Jar. The one to be 'edited' is opened and its contents enumerated. Any entries which are to be left alone are simply written unchanged to the new jar.

For your properties file, you could recreate the Properties object from the JarEntry
Code:
[COLOR=green]//jar is the open JarFile[/color]
JarEntry jarEntry = jar.getJarEntry("[i][b]property entry name in jar[/b][/i]");
Properties props = new Properties();
props.load( jar.getInputStream(jarEntry) );
You could make your changes to the props object and then write it to the new jar:-
Code:
[COLOR=green]//jarOut is a JarOutputStream to the new tmp jar[/color]
jarOut.putNextEntry(new JarEntry(jarEntry));
props.store( jarOut, null );
jarOut.closeEntry();

You then need to remove the old jar and rename/move the temporary one in its place.

I hope this helps.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top