4

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?

user1574866
  • 275
  • 2
  • 6
  • 12

3 Answers3

4

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.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
4

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

Baz
  • 36,440
  • 11
  • 68
  • 94
0

The best solution would be to use the JAR in your code and extend the class (if the class allows you to do it).

If you can't, the JAR class is actually a ZIP archive. You can rename x.jar to x.zip and extract the content:

Here you have more informations (see especially OscarRyz's answer): How do you recompile a jar file?

Community
  • 1
  • 1
mihaisimi
  • 1,911
  • 13
  • 15