tags:

views:

659

answers:

7

I would like to modify a file inside my jar. Is it possible to do this without extracting and re jarring, from within my application?

File i want to modify are configuration files, mostly xml based.

The reason i am interested in not un jarring is that the application is wrapped with launch4j if i unjar it i can't create the .exe file again.

A: 

short answer only: you cannot

dfa
This is incorrect, it is actually not difficult to do so using WinZip or some other similar application; see other posts.
seansand
the original question was about modifying a file of a Jar at runtime
dfa
A: 

As long as this file isn't .class, i.e. resource file or manifest file - you can.

Sorantis
+4  A: 

Java jar files are the same format as zip files - so if you have a zip file utility that would let you modify an archive, you have your foot in the door. Second problem is, if you want to recompile a class or something, you probably will just have to re-build the jar; but a text file or something (xml, for instance) should be easily enough modified.

JustJeff
A: 

To expand on what dfa said, the reason is because the jar file is set up like a zip file. If you want to modify the file, you must read out all of the entries, modify the one you want to change, and then write the entries back into the jar file. I have had to do this before, and that was the only way I could find to do it.

EDIT

Note that this is using the internal to Java jar file editors, which are file streams. I am sure there is a way to do it, you could read the entire jar into memory, modify everything, then write back out to a file stream. That is what I believe utilities like 7-Zip and others are doing, as I believe the ToC of a zip header has to be defined at write time. However, I could be wrong.

aperkins
A: 

IF your in a mac use Jarinspector, it does exactly what you want to do http://www.codeland.org/

As said previously, as long as the file isnt a .class file you can with that tool

Faisal Abid
+2  A: 

This may be more work than you're looking to deal with in the short term, but I suspect in the long term it would be very beneficial for you to look into using Ant instead of building jar's manually. That way you can just click on the ant file (if you use eclipse) and rebuild the jar.

Alternatively, you may want to actually not have these config files in the jar at all - if you're expecting to need to replace these files regularly, or if it's supposed to be distributed to multiple parties, the config file should not be part of the jar at all.

dimo414
Agreed. However, you might want to put a "default" configuration file in the JAR, so if something happens to the external one (corrupted, deleted, etc), you can fall back to a default configuration.
Thomas Owens
True - though I don't know how many applications would continue to function without their configuration files...
dimo414
+2  A: 

As many have said, you can't change a file in a JAR without recanning the JAR. It's even worse with Launch4J, you have to rebuild the EXE once you change the JAR. So don't go this route.

It's generally bad idea to put configuration files in the JAR. Here is my suggestion. Search for your configuration file in some pre-determined locations (like home directory, \Program Files\ etc). If you find a configuration file, use it. Otherwise, use the one in the JAR as fallback. If you do this, you just need to write the configuration file in the pre-determined location and the program will pick it up.

Another benefit of this approach is that the modified configuration file doesn't get overwritten if you upgrade your software.

ZZ Coder