• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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: 22819
132
Eclipse IDE Spring 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.
 
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic