File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes viewing and changing the files in .jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "viewing and changing the files in .jar file" Watch "viewing and changing the files in .jar file" New topic
Author

viewing and changing the files in .jar file

naresh kumar mallala
Greenhorn

Joined: Jun 13, 2011
Posts: 21
Is it possible to open and modify the ".jar file" using java code...if yes please give me approach? thank you!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  19

Check out the java.util.jar and java.util.zip packages.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

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

Joined: Jun 13, 2011
Posts: 21
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

Joined: Sep 20, 2010
Posts: 3615
    
  14

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

Joined: Jun 13, 2011
Posts: 21
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

Joined: Sep 20, 2010
Posts: 3615
    
  14

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

Joined: Jun 13, 2011
Posts: 21
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

Joined: Sep 20, 2010
Posts: 3615
    
  14

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

Joined: Jun 13, 2011
Posts: 21
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

Joined: Mar 17, 2010
Posts: 1051

Hope this may help


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 01, 2001
Posts: 2849
    
  11

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: viewing and changing the files in .jar file