Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

De-compile a JAR file to update a java file and re-archive the JAR file - am I doing it right?  RSS feed

 
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Questions:

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?

Thanks in advance for your assistance.

Lin
 
author
Sheriff
Posts: 23510
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your JDK should include a "jar" command that can be used to list, create, extract, and update the jar archive.

Henry
 
Sheriff
Posts: 23509
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 251
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can check below URL:
https://docs.oracle.com/javase/tutorial/deployment/jar/update.html

you can also try if given command also update existing file in jar
 
Saloon Keeper
Posts: 8907
167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lin Ahmad
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

C:\Tivoli\batch\tst>jar uf Test.jar packagePath1\packagePath2\packagePath3\..\..\..\xxx.class

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).
 
Lin Ahmad
Greenhorn
Posts: 9
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

This page provides me the answer: https://docs.oracle.com/javase/tutorial/deployment/jar/modman.html

Thanks to all, appreciate your help :-)

Lin
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!