1

I need to edit a single class file from within a jar.

I have successfully extracted the class file from the jar, and I have decompiled it and found the logic I need to change.

However, I'm unable to recompile this class file, because it imports libraries I don't have and don't know where to get (netbeans and iharder).

The needed files should all be within the jar, right? Can I use the jar for this purpose?

I do not understand much of Java's overarching syntax, so anything related to packages or jar file structure might go over my head...

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
Bo Thompson
  • 359
  • 1
  • 12
  • *The needed files should all be within the jar, right?* Wrong. – Robby Cornelissen Nov 02 '16 at 05:11
  • @RobbyCornelissen Could you explain? The class files in the jar should suffice for allowing the dependent classes to resolve. – 4castle Nov 02 '16 at 05:18
  • Answers to [This SO question](http://stackoverflow.com/questions/14069082/how-to-change-already-compiled-class-file-without-decompile) might help. – Sabir Khan Nov 02 '16 at 05:18
  • @4castle Actually, I've seen that SO question. I've installed Eclipse, but I can't follow the instructions of the answer on that page. The closest I got was an attempted compile of the unedited .class file, with immediate errors that the package name was incorrect. Must have set something up wrong...? – Bo Thompson Nov 02 '16 at 05:29

1 Answers1

0

The JVM just needs to be able to find the dependencies at runtime. Often, they'll be installed in a standard location (the classpath), rather than being bundled with the jar that uses them. However, you could theoretically even do something like download dependencies at runtime and load them via a classloader.

Apart from that, decompiling and recompiling is often not a good idea, since decompilation is a lossy and error prone process. It generally only works in simple cases, and has limitations, as you've discovered.

If you understand Java bytecode, you can edit the class by disassembling it with Krakatau, editing the .j file, and then reassembling. This allows you to edit any classfile without needing to compile, meaning you don't need the dependencies. It also works no matter how complicated the class is, and even works on obfuscated classes.

Antimony
  • 37,781
  • 10
  • 100
  • 107