I have a large pre-compiled project with lots of packages and class files. I have extracted one of the class files and decompiled it and edited some of the code inside. Now I would like to compile the changed code and re-insert it back into the original pre-compiled project, but unfortunately the code keeps many references to Objects in the pre-compiled project so I cannot compile without having it be already in the project which creates a rather large paradox. is there any for me to do what I am trying to accomplish?
Just compile it with a classpath which refers to the existing class files (or the jar file that contains those class files). There should be no problem.
However, note that if you change any constants in the file, those changes won't be reflected in any other code that refers to those constants.
It would generally be a much better idea to recompile from the complete source code. It would also be a better idea to use the original source code than just the result of decompilation - do you not have access to the original source code? (If you don't, are you sure that what you're doing is even legal in your country? I'm not a lawyer, but you should at least check...)
I would recompile the whole thing to avoid problems, but if you MUST, try this and let me know if they work for you:
Instead of loading the class on your original project, load it using classForName http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html
Remember that you cannot change the signature of the methods as this would indicate a different object since it wouldn't follow the same contract (interface).
Also keep in mind the serialVersionId What is a serialVersionUID and why should I use it?
Related
I'm trying to reverse engineering a .jar file (basically a group of .class files). However, there are two difficulties:
Some of the variables, methods and classes are named with unicode characters, which cannot be properly displayed. This makes source editing very inconvenient.
Some of the classes are named with Java keywords (such as if). Therefore Java compiler will complain when recompiling the reverse-engineered source files.
Are there any Java deobfuscators that can overcome these difficulties?
There are several tools that can rename Java identifers. For example, you can just run Proguard on it to rename everything.
That being said, I would suggest reconsidering your approach. Java compilation and decompilation are both lossy processes. Decompilation is useful for reverse engineering, but you generally cannot expect to be able to recompile the decompiled code. If it has previously been obfuscated, you may as well forget it.
The best way to modify obfuscated code is to use the Krakatau assembler and disassembler. Krakatau assembly can be reversibly transformed to and from bytecode, meaning you can edit arbitrary classes without messing things up, no matter how they have been obfuscated. The only downside is that you have to be familiar with Java bytecode.
Cuchaz's Enigma supports renaming local variables. It's no longer being updated and runs on BCEL which is also no longer being updated. As long as you're not decompiling anything BCEL can't handle it should work just fine though.
I am trying to take files from a jar that is part of a working project, and put them back in to the project so I can run it while making subtle changes to the classes.
I have read it is possible to extract a jar, decompile, edit, reassemble the jar and run the project, but I dont want to do all that every time I make a small edit.
I have tried extracting and decompiling the jar, and then creating a new package in eclipse with the same name as the original jar, and then adding all the files back in; however I get hundreds of errors.
I am very new to java and I realize this is beyond my current skill level, so any help is greatly appreciated if there is a simple way to do this. None of the other threads on this give a clear answer.
Compilation and decompilation are lossy processes, so in general, you can't expect to be able to re-compile decompiled code. If you want to make changes to an application and run the modified version, your best best is disassemble it with Krakatau, edit the assembly file, and reassemble. The Krakatau assembly format is designed to be very close to the classfile format, so you can make changes without disrupting everything. The downside is that you have to understand Java bytecode.
I'd also suggest checking out Konloch's Bytecode Viewer or Samczsun's Helios, which might be able to do what you want.
I am about to transform(manipulate) a lot of classes and to allow easy debugging and transparent communication of the changes applied to the code, I want to add java source code equivalent of the manipulated class.
In order to add java code I would be able to use existing source code and manipulate it in parallel. Then I need to store it along with the class.
Visiting the class file format for JDK 8, I noticed that no attribute exists to directly embed source code. Remembering the old times it was possible to include the source code within a class file. What do I miss? (I only found the attribute for specifying a source file). Also the option in the compiler tab of Eclipse does only show options to embed file names... .
Beside seaming to store source files separately, I wonder if it is feasible to reverse engineer (decompile) the class file. If one provide information about local names and parameter names, this might even be a better option.
The ASM documentation stated out that a tool is available to even decompile byte code.
Does anyone has some insights or experiences to share on this matter?
How to open and edit java class files? I've searched, but I either found how to open them without being able to edit them, or how to be able to edit only the bytecode. I want to be able to read and edit .class in the "normal" view. (as source code, not bytecode)
What you are asking for is basically impossible. The ".class" files do not contain source code, and do not contain enough information to reconstruct the original source code.
If you are a bit lucky, a good decompiler would be able to create compilable source code that means the same thing as the ".class" files. However:
That decompiled source code won't have the original comments.
The original names of any local variables are not recoverable.
The structure of the decompiled code may be different; e.g. string concatenation, for loops and try/catch structures may be transformed.
The code is not guaranteed to be correct, or compilable at all. (It depends on the decompiler, and how well it deals with the version of Java you are trying to decompile.)
And if the code you are trying to edit was obfuscated, then your chances of success are greatly reduced. An obfuscator deliberately transforms the ".class" files to remove useful information, and to confuse decompilers.
To my knowledge, no IDE supports editing of ".class" files like this.
Before my suggestion, I wanna point out that as far as I've searched, there has yet to exist a free decompiler that allows you to edit the source code produced, and save it automatically. I believe this is due to decompilers only being able to attempt to decompile the code, and the source code produced is not always exact/error free/compilable.
What you can do
You must use a decompiler, such as JAD, copy the source code that it produces and paste it into a new file.
As for the download link, you can find that on google, as I am unsure of the safest place to get it.
A decompiler does its best at converting the content (bytecode) within .class files into readable Java source code. Not only will some identifiers (method, variable and class names) will be replaced with generic names, meaning String name; in bytecode might decompile to String aString1;. Random variables might also be generated (tmp variables), which can lead to the produced code being unable to compile.
Whenever I build my app all classes (logically) are visible in the .jar that comes out of it.
Aswell as a class that holds information to my MYSQL server (for the app to connect to). But I dont want this information to be publicly visible!
How can I "hide" this code or "hide" the class?
Thanks!!
I think you mean you dont want someone to do reverse engineering with your .class inside your jar file. There are many decompilers that can do that.
So you would need to Obfuscate your code with an obfuscator utility.
The process of obfuscation will convert bytecode into a logical
equivalent version that is extremely difficult for decompilers to pick
apart. Keep in mind that the decompilation process is extremely
complicated and cannot be easily 'tweaked' to bypassed obfuscated
code. Essentially the process is as follows:
Compile Java source code using a regular compiler (ie. JDK)
Run the obfuscator, passing in the compiled class file as a
parameter. The result will be a different output file (perhaps with a
different extension).
This file, when renamed as a .class file, will be functionally
equivalent to the original bytecode. It will not affect performance
because a virtual machine will still be able to interpret it.
Here is an article describing this process in more detail and
introducing an early obfuscator, Crema:
http://www.javaworld.com/javaworld/javatips/jw-javatip22.html