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

Editing a .jar archive in a Java application.

Levi Webb
Greenhorn

Joined: Mar 13, 2012
Posts: 10
Hey, I've been attempting to make a patcher to download a .zip file, extract it, then add the files to a .jar archive. I whipped up a simple swing frame, and I've completed the functions for downloading and extracting the mod content to a folder. However using classes from the java.util.jar package don't seem to work properly when adding to a jar archive. Here's my code:



Here's the addToJar(String, ZipOutputStream) method:


(Yes, I used a try...catch around this.)

So, there's my code which runs perfectly fine. It runs into no errors at all, and seems like it added the files successfully. However, when I attempt to open the jar archive with File Roller (Ubuntu's default archive manager), it gives the following error:



And when running the game in which I was trying to patch (At this point you probably know it's minecraft), I get an error regarding the main class unable to be found. It looks like it's messing up the format of the jar file, in which I believe is actually a zip format, considering the contents of java.util.jar extend the contents of java.util.zip.

I don't care if I'm using external libraries, or if I have to spend hours learning how to add contents to a jar file. How do I do this, or what am I doing wrong? Anyone who can shed some light will be appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

I don't see where you close your JarOutputStream. If you don't close it then the data which is normally stored at the end of a ZIP archive (the directory) doesn't get written out, and the error message you posted does seem to be complaining about just that.
Levi Webb
Greenhorn

Joined: Mar 13, 2012
Posts: 10
Paul Clapham wrote:I don't see where you close your JarOutputStream. If you don't close it then the data which is normally stored at the end of a ZIP archive (the directory) doesn't get written out, and the error message you posted does seem to be complaining about just that.


I can't bother but reply with the following text:

THANKYOUTHANKYOUTHANKYOUTHANKYOUTHANKYOUTHANKYOUTHANKYOU

I've been troubleshooting much further than I needed to for such a stupid mistake. Thanks for pointing that out, you are my hero.
Levi Webb
Greenhorn

Joined: Mar 13, 2012
Posts: 10
Thanks for the first pointer. I now have have the content able to be installed into the .jar with it's former files. However, I still get the following error from minecraft when trying to run it with the modded jar:



Ironically, the class net.minecraft.client.MinecraftApplet actually exists, but the error claims it does not. I've also noticed after patching it, the size of the file increases dramatically (from 4.2MB to 5.2MB), even though I only edited a few text files for language configuration in the game, which shouldn't cause an increase of 1MB of data. After taking a deep look at ZipOutputStream, I've noticed there are setLevel(int) and setMethod(int) methods. I'm supposing these need to be changed to correspond with the .jar format for the JVM to recognize it's contents. If so, what are the values for these methods?, and if not, what am I doing wrong?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
I'm not quite sure what you're trying to do, but the java.util.jar and java.util.zip packages can't be used to edit existing jar/zip files.

Have you done a "jar tf" on the newly edited file to see if its contents differ in any unintended way from the original?


Ping & DNS - my free Android networking tools app
Levi Webb
Greenhorn

Joined: Mar 13, 2012
Posts: 10
Ulf Dittmer wrote:I'm not quite sure what you're trying to do, but the java.util.jar and java.util.zip packages can't be used to edit existing jar/zip files.

Have you done a "jar tf" on the newly edited file to see if its contents differ in any unintended way from the original?


I was trying to edit the contents of an executable jar file using a Java program. However, since you said those packages cannot be used to edit existing jar files, I changed the program to extract the data from the .jar, edit it with the contents of a downloaded .zip, then create a new .jar with the new edited contents. After this edit, the size of the .jar increased again, from 5.2 to 6.0MB (The original game is 4.6), and the JVM still couldn't find it's main class, despite that archiving programs were able to read the .jar perfectly fine, and extract enact data. I know the contents of the .jar are the same, but the size of the jar is different, probably due to compression format and level.

To dumb down what I need in a single sentence, I need to create an executable jar in Java. Is this possible without helper libraries?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

Levi Webb wrote:To dumb down what I need in a single sentence, I need to create an executable jar in Java. Is this possible without helper libraries?


I don't know what you mean by "helper libraries". The classes in java.util.jar are designed precisely to create executable jars.

And I would suggest you answer the question which Ulf Dittmer asked you.
Levi Webb
Greenhorn

Joined: Mar 13, 2012
Posts: 10
Paul Clapham wrote:
Levi Webb wrote:To dumb down what I need in a single sentence, I need to create an executable jar in Java. Is this possible without helper libraries?


I don't know what you mean by "helper libraries". The classes in java.util.jar are designed precisely to create executable jars.

And I would suggest you answer the question which Ulf Dittmer asked you.


I did. I created a new jar rather than adding to an old one. I simulated adding to the jar by extracting the .jar's contents, modding those contents, then creating a new .jar to put the new files. I will post my new code:



I hope anyone reading this can recognize that this is an action listener that creates a new thread to download the .zip, extracts the contents of the .jar, and then extracts the contents of the .zip into the .jar's extracted contents. After, it creates a new jar file with the new set of files. I'm creating a brand new jar here, but the JVM does not recognize any of the classes inside. What am I doing wrong?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

What are you doing wrong? Well, as we have said several times now, the way to find that out is to look in the jar file you created and see what's there. The code is only useful once you see what output it produces.
Levi Webb
Greenhorn

Joined: Mar 13, 2012
Posts: 10
Paul Clapham wrote:What are you doing wrong? Well, as we have said several times now, the way to find that out is to look in the jar file you created and see what's there. The code is only useful once you see what output it produces.


I honestly don't know. I can view, extract, and modify all the contents in the produced jar file with a archive program, but the JVM does not recognize the contents inside.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

Well, this thread is so long that I can't find what it is you're expecting to be in the jar file. And you apparently have looked in the jar file somehow, although you also haven't showed us what's there. So without those two things, we're just going to have to accept your statement that something isn't working and leave it at that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Editing a .jar archive in a Java application.