Easy Java .class file editor?
I have a Java class that is full of errors and won't even compile on my computer (I have no idea how this guy got it to compile) but somehow it works. I can't recompile it with my usual tools, so I'm looking for something that could compile it but maybe ignore errors, or what I thought would be cool would be able to open it in a program that decompiles and shows the files's source, allows me to edit it, and then automatically recompiles it. Kind of like JD-GUI but you can edit the files.
I don't think that such a tool exists for Java. Or at least, not in a form that will be effective in solving your problem.
A more productive approach is to find out what is causing those compilation errors and fix it. If you are getting a bunch of errors in a file that should compile (e.g. because it compiles for someone else), the chances are that it is one of the following:
- Your build classpath is incorrect, and the compiler cannot find the external dependencies as a result.
- You are missing some JAR files that contain external dependencies.
- You have the wrong versions of some JAR files.
- You are using the wrong version of the Java SDK.
These are all easy to fix ... once you know what the problem is.
If you provided us with a sample of the compilation errors, we could confirm this ... or offer a better diagnosis.
The current generation of decompilers are not very good at generating compilable source code. And besides, this approach won't fix any of the problems I listed above. (And even if it did, you'd be digging yourself into a very deep hole by hacking on decompiled source code.)
Although Java bytecode is slightly easier to decompile than languages that are compiled directly to executable code, Java decompilers are notoriously ineffective, especially when it comes to more complex code, and even more so if the original source code uses generics. The resulting source code often has no relation at all to the original, if it compiles at all...
If you have some human-generated version of the source code for that class I would suggest trying to fix that, rather than trying to reverse engineer a .class file.
If you have a .class file, you may still be able to extend or override some of its methods in a subclass, essentially modifying its behavior. Using inheritance like this is not always possible, but it often helps with third party black-box code...
You might want to have a look at Javassist. It is a bytecode manipulation library that, among other capabilities, it allows you to insert Java statements in existing methods. It does have quite a few constraints, but it can be very helpful in some cases...