17

So, I have third party library, which is a .jar file. There are some classes in that jar. The problem is, there is one bug in one class in that .jar. I know it because I can decompile the jar file, to look at the java code, which is I am pretty sure, that class is the source of my program bug.

The idea is, I delete the class and replace it with my own class, but I dont know how.

Agung Pratama
  • 3,666
  • 7
  • 36
  • 77

3 Answers3

20

There are multiple ways to do this:

  1. Try to use winrar. You can open your jar in it, explore the directory containing the class file. You can delete and add class files.

  2. If you don't want to use winrar then do like this:

Extract the jar using this command

jar -xvf yourjar.jar

It will explode the jar. Delete the old class file and add your updated class file

Recreate the jar using the following command

jar -cvf yourjar.jar directoryofexploderjar/

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • one more approach using winzip/winrar open your jar, go to the class location and just drag and drop updated class, close winrar/winzip and do not need to explod jar and then recrete jar... – Alpesh Gediya May 29 '13 at 05:40
  • ** jar uf <> <>** – Alpesh Gediya May 29 '13 at 05:47
  • 1
    Worth noting that the resulting jar will have all classes nested under `directoryofexploderjar/` with these commands. – austin_ce Jun 24 '20 at 16:58
  • Also worth noting, on MAC if you explode the JAR into the current directory, and then delete the original JAR file, the OS will create the hidden `.DS_Store` file. If that file gets repackaged into the "new" JAR, it may be corrupt (ex: Android Studio will not recognize it) - be sure to manually remove the `.DS_Store` file :) – HelloImKevo Mar 12 '21 at 23:03
1
  1. Extend class and rewrite method removing bug

  2. Use JDEC to decompile and replace class ( http://jdec.sourceforge.net/ )

Igor S.
  • 3,332
  • 1
  • 25
  • 33
0

Write your own class in the same package as the original one, make sure your classes are before the 3-rd party jar on the java classpath. This way you will override the original version, class loader will load your class.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275