• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Writing to a file inside a jar file

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way I can modify the contents of a file inside a jar file? I have tried different options but haven't succeeded. I just have a war file that does not have any contents inside the classes folder, but has all the working code inside the lib folder (jars). One of these jar files say (my1.jar) has a text file say (sample.txt) packaged along with it. I am able to read the contents of the file (sample.txt) from the code in my1.jar using (getClass().getResourceAsStream("path to sample.txt")). I just am curious to know whether I will be allowed to modify the content of the file (sample.txt).

my.war
|
|---META-INF
|
|---WEB-INF
|
|-- classes (empty)
|
|--lib
|
|---my1.jar
|---my2.jar

my1.jar
|
|--- META-INF
|--- com
|--my
| |
| |--file
| |
| |--sample.txt
|--MyClass (I am able to read the contents of the file from this class)

I am not able to create a file object of 'sample.txt' file. The Url obtained just does not start with 'file://..'(hierarchial) its opaque as ('jar:file://....'). I dont have a way to created a output stream either!

Any help is greatly appreciated.

Regards







 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's simply not possible. JAR (and ZIP, which is the basis of JAR) doesn't allow actual writing into it. Any tool that apparently allows you to do this will create a temporary file with the new contents, then rename the temporary file to the old file.

With JAR files this is going to be even a bit harder, since the JVM locks the JAR files it loads. You can't modify or replace them until after the JVM exits.
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic