• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

viewing and changing the files in .jar file

 
naresh kumar mallala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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!
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the java.util.jar and java.util.zip packages.
 
Stephan van Hulst
Bartender
Pie
Posts: 6113
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Pie
Posts: 6113
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Pie
Posts: 6113
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Pie
Posts: 6113
73
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic