Modifying a file inside a jar

JavaJar

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

You can use the u option for jar

From the Java Tutorials:

jar uf jar-file input-file(s)

"Any files already in the archive having the same pathname as a file being added will be overwritten."

See Updating a JAR File.

Much better than making the whole jar all over again. Invoking this from within your program sounds possible too. Try https://stackoverflow.com/questions/8496494/running-command-line-in-java

Solution 2 - Java

You can use Vim:

vim my.jar

Vim is able to edit compressed text files, given you have unzip in your environment.

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

Not sure if this help, but you can edit without extracting:

  1. Open the jar file from vi editor
  2. Select the file you want to edit from the list
  3. Press enter to open the file do the changers and save it pretty simple

Check the blog post for more details http://vinurip.blogspot.com/2015/04/how-to-edit-contents-of-jar-file-on-mac.html

Solution 6 - Java

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 (or Maven, or even Bazel) 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.

Solution 7 - Java

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.

Solution 8 - Java

Yes you can, using SQLite you can read from or write to a database from within the jar file, so that you won't have to extract and then re jar it, follow my post http://shoaibinamdar.in/blog/?p=313

using the syntax "jdbc:sqlite::resource:" you would be able to read and write to a database from within the jar file

Solution 9 - Java

Check out TrueZip.

It does exactly what you want (to edit files inline inside a jar file), through a virtual file system API. It also supports nested archives (jar inside a jar) as well.

Solution 10 - Java

Extract jar file for ex. with winrar and use CAVAJ:

Cavaj Java Decompiler is a graphical freeware utility that reconstructs Java source code from CLASS files.

here is video tutorial if you need: https://www.youtube.com/watch?v=ByLUeem7680

Solution 11 - Java

most of the answers above saying you can't do it for class file.

Even if you want to update class file you can do that also. All you need to do is that drag and drop the class file from your workspace in the jar.

In case you want to verify your changes in class file , you can do it using a decompiler like jd-gui.

Solution 12 - Java

I have similar issue where I need to modify/update a xml file inside a jar file.

The jar file is created by a Spring-boot application and the location of the file is BOOT-INF/classes/properties

I was referring this document and trying to replace/update the file with this command:

> jar uf myapp.jar BOOT-INF/classes/properties/test.xml

But with this, it wont change the file at the given location. I tried all the options also but wont work.

Note: The command I am executing from the location where jar file is present.

The solution I found is:

  1. From the current location of jar file, I created folders BOOT-INF/classes/properties
  2. Copy the test.xml file into the location BOOT-INF/classes/properties.
  3. Run the same command again. jar uf myapp.jar BOOT-INF/classes/properties/test.xml

The xml file has been changed in the jar file.

Basically you need to create a folder structure like where the file is located into the jar file. Copy the file at that location and then execute the command.

The problem with the documentation is that, it does not have enough examples as well as explanation around common scenarios.

Solution 13 - Java

The simplest way I've found to do this in Windows is with WinRAR:

  1. Right-click on the file and choose "Open with WinRAR" from the context menu.
  2. Navigate to the file to be edited and double-click on it to open it in the default editor.
  3. After making the changes, save and exit the editor.
  4. A dialogue will then appear asking if you wish to update the file in the archive - choose "Yes" and the JAR will be updated.

Solution 14 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHamza YerlikayaView Question on Stackoverflow
Solution 1 - JavaclwhiskView Answer on Stackoverflow
Solution 2 - JavaJose AlbanView Answer on Stackoverflow
Solution 3 - JavaJustJeffView Answer on Stackoverflow
Solution 4 - JavaZZ CoderView Answer on Stackoverflow
Solution 5 - JavaBuvin PereraView Answer on Stackoverflow
Solution 6 - Javadimo414View Answer on Stackoverflow
Solution 7 - JavaaperkinsView Answer on Stackoverflow
Solution 8 - JavashoaibView Answer on Stackoverflow
Solution 9 - JavaGreenGiantView Answer on Stackoverflow
Solution 10 - JavaTomasz MularczykView Answer on Stackoverflow
Solution 11 - JavaSu SoView Answer on Stackoverflow
Solution 12 - JavaAtulView Answer on Stackoverflow
Solution 13 - JavaSteve ChambersView Answer on Stackoverflow
Solution 14 - JavaSorantisView Answer on Stackoverflow