You can edit .class files, but not in a text editor — they are compiled binary code, not human-readable text
A .class file is the compiled output of Java source code. When you write a .java file and compile it, the Java compiler turns it into a .class file containing bytecode — instructions for the Java Virtual Machine (JVM) to run. Because .class files are binary, opening one in Notepad or VS Code shows gibberish. To make changes, you need either the original .java source file or a tool that can decompile and recompile the bytecode.
The practical choice depends on what you are trying to do. If you have the source code, edit the .java file and recompile. If you only have the .class file and need to change it, you will need a decompiler to convert it back to readable Java code, then use a bytecode editor or recompile from the recovered source.
Key Takeaways
- .class files contain compiled bytecode and cannot be edited directly in a text editor — you need either the original .java source or a decompiler.
- The fastest route is to find and edit the original .java file, then recompile it using javac or your IDE's build tool.
- If you only have the .class file, use a decompiler like CFR, Procyon, or JD-GUI to recover the source code first.
- Bytecode editors like Bytecode Viewer or ASM can modify .class files directly without decompiling, but require understanding of Java bytecode structure.
- Always keep a backup of the original .class file before making changes, since mistakes in bytecode editing can break the file.
Editing the original .java source file and recompiling
This is the standard and safest approach. If you have access to the .java file that was compiled into the .class file, edit that instead. Open the .java file in any text editor or IDE — Visual Studio Code, IntelliJ IDEA, Eclipse, or even Notepad work fine. Make your changes to the source code, save the file, and recompile.
To recompile from the command line, navigate to the folder containing the .java file and run javac FileName.java. The compiler will generate a new .class file with your changes. If you are using an IDE like IntelliJ or Eclipse, the build happens automatically when you save, and the .class file updates in the output folder (usually called bin or target).
This method is reliable because you are working with readable code, and the compiler catches errors before the .class file is created. The resulting .class file is may provide to be valid bytecode.
Using a decompiler to recover source code from a .class file
If you only have the .class file and no source code, a decompiler converts the bytecode back into Java code you can read and edit. Common decompilers include CFR, Procyon, JD-GUI, and Fernflower. Each has strengths depending on the Java version and features used in the original code.
CFR is widely used and handles modern Java syntax well. To use it from the command line, download the CFR jar file and run java -jar cfr.jar FileName.class. The decompiler outputs the recovered .java code to the terminal or to a file if you add --outputdir src to save it. Once you have the .java file, edit it as you would normally, then recompile with javac.
Decompilation is not always perfect — obfuscated code, complex generics, or very new Java features may not decompile cleanly. But for most straightforward .class files, you will get readable code that you can modify and recompile.
Editing bytecode directly with a bytecode editor
If you want to modify a .class file without decompiling it, a bytecode editor lets you change the compiled instructions directly. Bytecode Viewer is a free tool that shows the bytecode, decompiled code, and hex view side by side, and lets you edit the bytecode directly. ASM is a Java library for reading and writing bytecode programmatically, useful if you are writing a tool to modify .class files automatically.
This approach is advanced and requires understanding Java bytecode — the low-level instructions the JVM executes. A single mistake in the bytecode can render the .class file unusable. Most developers avoid this unless they are working on bytecode manipulation frameworks or need to make very small, surgical changes that decompiling and recompiling would not handle well.
If you go this route, always keep a backup of the original .class file. Test the modified file thoroughly before deploying it, because errors in bytecode are harder to debug than errors in source code.
Using an IDE to decompile and edit in one workflow
Modern IDEs like IntelliJ IDEA and Eclipse can decompile .class files on the fly. In IntelliJ, if you open a .class file directly, the IDE decompiles it and shows you readable code. You can read it but not edit it directly in that view. However, you can copy the decompiled code, create a new .java file, paste it, make your changes, and recompile.
Some IDEs also have plugins that extend this workflow. IntelliJ's built-in decompiler (Fernflower) is reliable for most code. If the decompilation is incomplete or incorrect, you can switch to a different decompiler by configuring the IDE settings.
What happens when you modify a .class file
When you change a .class file — whether by decompiling, editing, and recompiling, or by editing bytecode directly — the JVM will load and run the new version the next time the class is used. If the .class file is part of a running application, you usually need to restart the application for the changes to take effect. Some frameworks support hot-reloading, which reloads classes without restarting, but this is not standard behavior.
If the modified .class file is in a JAR archive, you need to extract the JAR, replace the .class file inside it, and repackage the JAR. Tools like jar (the command-line tool) or any ZIP utility can do this, since JAR files are ZIP archives.
Common problems and how to avoid them
The most common mistake is editing a .class file and then trying to use it without testing. If the bytecode is malformed, the JVM will throw a ClassFormatError or VerifyError when it tries to load the class. Always test the modified .class file in a safe environment before using it in production.
Another issue is version mismatch. If the original .class file was compiled for Java 8 but you recompile the decompiled source with Java 17, the bytecode version changes, and it may not work in environments expecting the original version. Check the target Java version before recompiling.
Obfuscated .class files are harder to decompile and modify. If the original code was run through an obfuscator, the decompiled code will have meaningless variable and method names, making it difficult to understand what to change. In this case, bytecode editing or reverse-engineering the obfuscation is necessary.
Frequently Asked Questions
Can I open a .class file in a text editor?
You can open it, but you will see binary garbage because .class files are compiled bytecode, not text. Use a decompiler to convert it to readable Java code first, or use a bytecode viewer that understands the binary format.
What is the difference between decompiling and disassembling?
Decompiling converts bytecode back to Java source code, which is readable and editable. Disassembling converts bytecode to assembly-like instructions (bytecode mnemonics), which is lower-level and harder to work with. For most edits, decompiling is the better choice.
If I edit a .class file, will it still work with the rest of my application?
Only if the changes are compatible with how other parts of the application use that class. If you change a method signature or remove a public method that other code depends on, you will get runtime errors. Test thoroughly before deploying.
Can I edit a .class file that is inside a JAR?
Yes, but you need to extract the JAR first, replace the .class file, and repackage it. Use the jar command or a ZIP tool: jar xf archive.jar to extract, then jar cf archive.jar to repackage after making changes.
What if the decompiler produces code that will not recompile?
This happens with complex or obfuscated code. Try a different decompiler — CFR, Procyon, and JD-GUI handle different code patterns differently. If decompilation fails, bytecode editing is your only option, though it is more difficult.