Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • 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: 22644
123
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.
 
Their achilles heel is the noogie! Give them noogies tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic