Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the code of the class from jar file

Tags:

java

jar

How can I read (load) the java class from jar file, add some code to the class and then put back to the jar? I need to write a script (a java code) which does all these things. Steps I think of are: 1. Read the jar and get the entry with the name of class by using methods from java.util.jar package 2. Load this class, get the array from the class, change it 3. Maybe compile the code to apply the changes 4. Put the renewed class to the jar file

I dont know how to do 2nd and 3rd steps in java.

I was thinking to write new java class which extends this class and change the array, but this array is declared as public static final, thats why I cannot change it. Or I am wrong and there is a way of changing the array by extending the class?

like image 679
user1574866 Avatar asked Feb 19 '23 14:02

user1574866


2 Answers

A jar is just a ZIP file, so you can extract it with any archive manager like 7-ZIP. It will contain class files, which are compiled Java files. You need a Java decompiler to read and modify the bytecode.

like image 158
Sjoerd Avatar answered Mar 06 '23 05:03

Sjoerd


Extract -> Decompile -> Add code -> Create Jar

like image 31
Baz Avatar answered Mar 06 '23 04:03

Baz