A Brief Introduction…
It all started when a co-worker asked for help when trying to update an IP Address of a java API, however, he only had the JAR file available. With another co-worker, we dug up the internet and tried to find a way to do this, and we finally did. I thought of sharing what we acquire for any future help.
In order to do any of the operations mentioned in the title, you will need to have java and JDK installed, a desired jar you want to modify, and basic knowledge of commands for the terminal. In order to get the .java file from the JAR, I would recommend a program called JD-GUI. You will just need to open the jar file, and look for a desired file and save it.
For this tutorial, we will be using the linked list jar we created in another tutorial. As we open the jar file with JD-GUI, I will choose to add another constructor in the Node.java class.
After saving it, we will be opening and modifying the java class with your favorite Java Editor. If you noticed, a jar is composed of .class files, which contains pure byte code and we wouldn’t be able to read it that easy. We will convert the .java file to .class through the command line and with the javac command: javac Node.java
Note: I am following the same folder structure as the JAR file. linkedlist>Node.class.
Until now, we have modified and generated the Node.class file, so we are just missing update the current file in the JAR. In order to do this, we will need to use the jar uvf command: jar uvf LinkedList.jar linkedlist/Node.class. The -u in the command means that we will be updating/inserting a specific file inside the JAR.
Ta-da! You just have modified and update a JAR file in a very simple way. Please open the jar file using JD-GUI again and check if the changes were done correctly.
If you wish to extract a specific file without requiring JD-GUI, it will be with using the -x argument within the jar command: jar xvf LinkedList.jar linkedlist/Node.class .
I hope you have learned how to modify and update any JAR file. Please let me know if you have any other further questions or comments.