Reputation: 5030
I have a JAR file and there is a static class, I can't decompile it, it gets messed up. There is one method in it, that returns a class, that doesn't exist. No idea how, but I'm getting NoClassDefFoundError
. Even though I'm not using that method, it still crashes. So I need to somehow just remove it and it should work fine. I know Reflection or Javassist could be used, but no idea how.
Note, that this is obviously not for production release, just some white-hat hacking.
Upvotes: 1
Views: 1416
Reputation: 140427
I think you are mistaken: if that method would never be called, then the JVM would most likely not try to load that class.
You see, the whole point of class loading - it happens lazy; when you load class X, and X needs Y, Z ... then Y and Z are only loaded when some code is executed that requires Y, Z to be loaded.
In other words: that NoClassDefFound error actually tells you that something (maybe a static init somewhere in your JAR) is calling that method.
Thus, the solution is not to "cut out" that one method (that would simply lead to another exception, like MethodNotFound!). Instead, you should try to create a dummy class to not run into that exception at all. Or: your JAR is missing at least one of its dependencies. You can try to resolve that dependency - maybe you can find the class somewhere, or, you "build" your own version of it (the second idea might be probably hard to get correct).
Upvotes: 2
Reputation: 39451
GhostCat already explained why your problem is not removing a method. But I figured I should answer the original question in case anyone else was wondering.
The best way to remove a method from a classfile is to disassemble it with Krakatau, delete the method you want from the .j
file, and reassemble it. This method allows you to edit any classfile, no matter how strange. (If you find any valid classfiles that the Krakatau disassembler can't handle, please file a bug).
When disassembling, you may also want to pass the -roundtrip
option. This isn't required for correctness, but it prevents the constant pool from being rearranged, reducing the binary diff of the resulting classfile. On the other hand, -roundtrip
also makes it a lot harder to locate the method you want in the .j
file.
Upvotes: 1