PDA

View Full Version : How to modify a java .jar file?




dudleybrooks
Apr 15, 2011, 01:03 AM
I need to make a simple modification in an application (Thunderbird.app) -- changing a single line in a .jar file. On my old Windows computer I knew what software to use. What's the easiest way in Snow Leopard to open, modify, and save the .jar file in the app? Preferably using things that are built into the OS; if not, then using freeware?



jiminaus
Apr 15, 2011, 02:07 AM
A .jar file is just a .zip file with a different extension (and some standard/predefined layout). You could use the standard zip and unzip programs in /usr/bin to change a .jar file, assuming it's not signed.

"Changing a single line in a .jar file" doesn't directly make sense in the context of .jar file. If you tell us exactly what you want to do with that .jar file, we might be able to tell you what Mac OS X tools/programs you can use and how.

mufflon
Apr 15, 2011, 03:15 AM
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; sv-se) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5)

There are really three phases: extraction, modification and assembling it back together.

Extracting is easy: right click and click unarchieve (afaik).

Modification: if it's the manifest (whick class to run and so forth) then it's easy, however the java files are compiled into .class files and will need disassembling before modification.

To piece everything together google the "jar" command for the terminal.

If it's FOSS it's better and easier to compile from source; if not then I don't think it's 100% legit.

dudleybrooks
Apr 15, 2011, 11:17 AM
I need to change a line in a .js file contained in the .jar file. But your reply clarifies my question:

I made this change many, many times under Windows (namely every time I downloaded an upgrade, which, of course, wiped out my previous change). The person who recommended the change also recommended an (un)archiver called IZArc, in which the whole process could be done from inside IZArc -- open (unarchive) the .jar file, find the .js file, edit the line ... and then clicking Quit would return everything to its archived state.

I understand that I can unarchive the .jar with any unarchiver and edit the .js with any text editor. I guess my worry is that, not being a programmer, I might do something harmful in re-archiving -- not archive all the relevant files, or archive too many files, or something.

So my question is: Is there an archiver (hopefully in the OS) which will do all the above from within itself, so that I know that the final .jar contains exactly the files that it should contain? Or, failing that, what do I need to do to make sure that I have re-archived it properly? I realize that in this forum this is a complete newbie question.

notjustjay
Apr 15, 2011, 12:11 PM
Well, if .jar files are simply ZIP files with a different extension, then you can zip and unzip them freely from the Terminal using the "zip" and "unzip" commands.

chown33
Apr 15, 2011, 12:45 PM
Well, if .jar files are simply ZIP files with a different extension, then you can zip and unzip them freely from the Terminal using the "zip" and "unzip" commands.

Or use the 'jar' command, which is written expressly for this purpose, and maintains the correct ordering for manifests (which typically must reside as the first entry, or at most one of the first few).

I'm pretty sure the jar command is installed by default. This should continue to be true on 10.7 Lion, as well.

notjustjay
Apr 15, 2011, 01:11 PM
Or use the 'jar' command, which is written expressly for this purpose, and maintains the correct ordering for manifests (which typically must reside as the first entry, or at most one of the first few).

I'm pretty sure the jar command is installed by default. This should continue to be true on 10.7 Lion, as well.

Ah, even better. Thanks, didn't know about that.

pilotError
Apr 15, 2011, 06:48 PM
Or you can use the jar command. The parameters are just like the command line zip utilities

$jar -xvf jarfile.jar ** Extracts the files

** edit the file **

$jar -cvf jarfile.jar ** Create the new Jar file

dudleybrooks
Apr 15, 2011, 07:44 PM
Thanks. PilotError's method was straightforward and successful -- no software errors, newbie errors, or even pilot errors. But it still had more steps than I used to need in Windows: switching from Terminal after unarchiving, to the desktop to use an editor (my fault, admittedly, for not knowing the unix editor(s) which I assume Terminal has access to), then back to Terminal to rearchive -- plus the more keystrokes needed for the unix commands. I'd still like to find an editor which does it *all* inside one program, like IZArc did in Windows: ctrl-open, edit, ctrl-close -- zip-zip (pun intended)!

chown33
Apr 15, 2011, 08:16 PM
Thanks. PilotError's method was straightforward and successful -- no software errors, newbie errors, or even pilot errors. But it still had more steps than I used to need in Windows: switching from Terminal after unarchiving, to the desktop to use an editor (my fault, admittedly, for not knowing the unix editor(s) which I assume Terminal has access to), then back to Terminal to rearchive -- plus the more keystrokes needed for the unix commands. I'd still like to find an editor which does it *all* inside one program, like IZArc did in Windows: ctrl-open, edit, ctrl-close -- zip-zip (pun intended)!

I don't know of any tool that does everything. Frankly, this is the first time I've ever seen anyone ask how to edit a file within a jar on Mac OS, so I'm guessing there isn't a big target market.

You can paste command-lines into Terminal, so just save the commands into a text file, then copy and paste away. No typing needed.

Here's roughly what I would save in the text file:
jar -xvf jarfile.jar ## Extracts the files

open -e path/to/fileYouWantToEdit.itsExtension

jar -cvf jarfile.jar ## Create the new Jar file

You should obviously change the jarfile.jar to the actual jar file you have.

I've shown ## because those are the actual comment character for bash.

I've also shown an 'open -e' command with a pathname you will have to substitute. The open -e will open the named file in TextEdit.app. If you have another editor you prefer instead, you can tell 'open' to use it instead. Here's the man page for the 'open' command:
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/open.1.html
Look at the -a and -b options in particular.

Finally, you can put other commands in there, such as a 'cd someDir' or whatever else you want.

You could probably write a 'sed' script to do the replacement, so you wouldn't have to manually edit the file at all. Then you could make the whole thing into a shell script, wrap that into an AppleScript and save it as a droplet. Then in future, you simply drop your original app onto the AppleScript droplet and it would do all the changes automatically. You'd have to write and debug all that stuff, though, and it would probably take hours more time than just doing a copy/paste when the app gets updated.

If the change to the jar is something the vendor should provide, you should probably file a bug-report against the app. Because the cleanest fix is to not have to fix it yourself at all.

pilotError
Apr 15, 2011, 08:44 PM
On windows, you can use programs like pkzip or winzip that show the contents in the window and you can just double click a file which then gets extracted under the hood and sent to the associated program (editor).

There are zip utilities under OS X, but admittedly, I don't use OS X like I use windows. I'm sure you could track one of the programs down that will allow you to do the same thing, but for what was needed here, it wasn't worth searching for it.

dudleybrooks
Apr 16, 2011, 09:42 AM
PilotError and Chown33: I'm sure you're right about the lack of market in OS X.

Chown33: Thanks for the advice about automation and the details involved. I will try them. OMG, I might become a programmer!

It's something *I* think should be fixed, but it's not a bug: Thunderbird has an Advanced Search facility for e-mail addresses, which might return multiple addresses. If you click Write, it puts them all into "To" fields (with no other option). The change makes it put them into "BCC" fields, for better netiquette. TB 1.x used to have this option, but lost it in 2.x.

peregin55
May 8, 2011, 09:00 PM
On Linux and Mac OS X I use emacs to edit files within a jar. Just type:

emacs somejar.jar

You can then browse all the files in the jar, open, save, whatever using standard emacs commands. Any changes you make will automatically be applied to the jar (no need to manually unjar anything)

dudleybrooks
May 8, 2011, 09:51 PM
peregin55 --

That's good to know.

--
Dudley

bigdisaster
Jun 8, 2012, 12:55 PM
argh. it doesn't work. I need to replace a few string in the .class files (I use exactly the same number of symbols), then I try to repack it with jar command, but the resulting file is a few bytes smaller than the initial one, and when I try to install it on my mobile it says 'wrong jar format'.
Any ideas?