I've a JAR file (which I will call the original JAR file) which includes some java classes and some other jar files. I need to extract one of the java classes to make some changes to it and put it back into the original JAR file.
I am thinking to de-compile the original JAR file using a Java De-compiler, and extract this java class. This will get me the java file. After making the changes for this java file, I'll recompile this java file, and will add the class file back to the original JAR file.
1. Am I doing it right to de-compile the original JAR file just to get the java file I need?
2. If so, I need to re-archive the original JAR file with this newly updated java file, how do I do that? Is there a tool to re-archive?
Lin Ahmad wrote:1. Am I doing it right to de-compile the original JAR file just to get the java file I need?
Well no, you'd be much better off to use the source code which was used to create the Java class in the first place. That's because decompilers aren't perfect and they can produce some pretty ugly code. It might be hard for you to make the required change to that ugly code. And it's even possible that the decompiler could produce source code which wasn't equivalent to the original class, or which wouldn't even compile. You'd have to have some way of testing the result of your decompile/recompile process to make sure that you didn't break anything.
But perhaps you don't have that source code? Then you would still be better off finding the person who originally created the class and asking them for the source code. If it's just lost then using the decompiler would probably be acceptable, but if they refuse to give it to you then you should definitely find out whether you are legally allowed to decompile the code before you start doing that.
Keep in mind that jar files are just zip archives with a different extension. You can open them and modify them with your favorite zip tool, without even having to change the extension.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
posted 2 years ago
Thanks for all your prompt replies, and for the link to the tutorial.
The JAR file does not have any source files, and has only class files. I've the source file for this file that I am trying to update. I updated this source file [xxx.java], and compiled it [xxx.class], and then updated the JAR file with this:
The package path: packagePath1\packagePath2\packagePath3\..\..\..\ to the xxx.class file is the same as the directory path of the xxx.class file in the JAR file.
The above jar command ran good - no error.
Question: How do I know that the class file was changed? After I ran "jar tfv Test.jar", the time on the class file did not change, only the time of the JAR file changed - it was updated to today's date (1/25).
posted 2 years ago
Now, I know how to add the class file!
I extracted the original JAR file, removed the META-INF directory, the MANIFEST.MF file and the xxx.class from this original JAR file. I then recreated the JAR by adding a new xxx.class and a new MANIFEST.txt file that I had created using the original MANIFEST.MF file. The command:
The jar command automatically creates the META-INF directory and puts the MANIFEST.MF file in.