I compiled a class file and want to inject it into an existed jar file. I tried but "jar" command is not a good solution, anyone can help on this?
Asked
Active
Viewed 7,939 times
-1
-
5_"jar" command is not a good solution_ why ? – jmj Aug 30 '12 at 08:42
-
You tried "jar" and it didn't work ? Did you get error msg ? What exactly is the problem with "jar" ? – justSaid Aug 30 '12 at 08:54
-
An alternative is to create a `patch.jar` which you place earlier in your class path. – Peter Lawrey Aug 30 '12 at 09:07
-
Basically, adding a file to an existing Jar file is (essentially) the same as adding files to an existing zip file. Take a look here for an example http://stackoverflow.com/questions/3048669/how-can-i-add-entries-to-an-existing-zip-file-in-java – MadProgrammer Aug 30 '12 at 09:10
-
@MadProgrammer , I'm trying you way. Thanks – JerryCai Aug 30 '12 at 09:34
-
@JigarJoshi, jar is a little bit environment dependent, code is more cross-platform – JerryCai Aug 30 '12 at 09:37
-
How jar is platform dependent ? – jmj Aug 30 '12 at 09:39
-
@JigarJoshi , jar.exe in Windows and jar in Linux – JerryCai Aug 30 '12 at 09:53
2 Answers
0
so I think you can decompress the jar, and copy your class file in the directory you compressed. and re compress these files into a new jar file.

user1203650
- 300
- 2
- 3
-
I also thought of this solution, but compare to inject a file to a existed jar, this way is risky. (Consider Manifest file, too big jar file, etc) – JerryCai Aug 30 '12 at 09:36
0
If you want to automate it in a script jar
is your best bet. It's installed with the JDK and gets the job done.
If it's a one time operation you might want to use e.g. 7-Zip or another archiving software that can handle the jar file format (which is basically a ZIP).

Dude
- 583
- 2
- 9