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

viewing and changing the files in .jar file

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to open and modify the ".jar file" using java code...if yes please give me approach? thank you!
 
Sheriff
Posts: 22783
131
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
Check out the java.util.jar and java.util.zip packages.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. A .jar file is simply a file, so you can change it just as you would change other files.

For jar specific I/O, you can use the JarFile, JarInputStream and JarOutputStream classes.

[edit]

Too slow :P
 
naresh kumar mallala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok....here i want to replace some class file in ".jar file" with some other class file!!! I think it's not a simple process like replacing a file in a "folder" with another file...Right???
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read out the contents of the jar file, and then write a new jar file (instead of writing back the old class files, you write their replacements).
 
naresh kumar mallala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi stephan,
here the problem is my jar file contains more than 10 folders with tree structure means each folder have some other folders.....and at last point i have more than 50 classes in my jar file in different folders.....it is not possible to create a new jar here....is there any way to search my specific class in that jar and replace it with some updated class???
thank you!
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you can't search for an entry and replace it. At least, not in any easy way that I know of.

Why does the tree structure prohibit you from creating a new Jar file? Simply read the Jar entries and if they have a particular name, write out a different one than the original.
 
naresh kumar mallala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok....for simply reading jar contents and write whenever I want change is the good process but what is the need of creating a new jar file....why we can't do the same process with existing one?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you can't replace single entries, I don't think. Maybe someone else can propose a way in which you can, but it probably won't be easy.

This means you have to write the entire file every time you want to change an entry. Sure, you can overwrite the original file. But this isn't particularly safe. What if an error happens? Your original file will be destroyed. A better way is to create a new file, and when this has succeeded, you can completely replace the original jar with your new one.

Frankly, this all sounds unnecessary to me. Why do you want to change classes in an existing jar?
 
naresh kumar mallala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually my role is deploying jar files in server.....everybody change some class file and they send me to replace the class in jar with updated one.....every time for replacing class file I have to go the path of that class and i put some backup and then i replace it....this leads a big problem to me.....thats why I am trying to do some automatic tool for all this!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this may help
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naresh kumar mallala wrote:Actually my role is deploying jar files in server.....everybody change some class file and they send me to replace the class in jar with updated one.....every time for replacing class file I have to go the path of that class and i put some backup and then i replace it....this leads a big problem to me.....thats why I am trying to do some automatic tool for all this!



cannt understand this..what do you mean is it replacing class in .jar file directly..or replacing complete jar file
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naresh kumar mallala wrote:Actually my role is deploying jar files in server.....everybody change some class file and they send me to replace the class in jar with updated one.....every time for replacing class file I have to go the path of that class and i put some backup and then i replace it....this leads a big problem to me.....thats why I am trying to do some automatic tool for all this!



But there are already plenty of automatic tools for building and deploying applications. There's no reason for you to write yet another automatic tool.

And your idea that you should try to modify an existing jar file rather than just creating a new one is, um, not a good idea. Just create a jar file from the class files.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agrees with Paul. It sounds like your role is configuration management, and you are trying to reinvent the process. The developers need to be putting their source code (not class files) in a source code repository, like SubVersion, CVS, Git, or many others. The jar should be created with a build tool like Ant or Maven.
 
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic