I am currently working on a project which demands me to decompile a .class file to java file programmatically. I.E. I have a program that should read a class file and decompile it and the resultant java source code is written into a file. Please help me out to do it.
EDIT:
I am completely new to the world of decompilers. I have gone through a few APIs, but i don't know precisely how to use and which one to use. Any sort of help will be really appreciable
EDIT:
I tried using:
import com.strobel.decompiler.*;
import java.io.*;
public class JavaDecode {
public static void main(String[] args)throws Exception {
decompileee();
}
private static void decompileee()throws Exception
{
final DecompilerSettings settings = DecompilerSettings.javaDefaults();
final FileOutputStream stream = new FileOutputStream("C:/jp/decompiled.java");
final OutputStreamWriter writer = new OutputStreamWriter(stream);
Decompiler.decompile("C:/jp/X.class",
new PlainTextOutput(writer),
settings
);
System.out.println("Success");
}
}
But the above code simply created a file called "decompiled.java" in the scpecified directory. But the file is an empty file.
Procyon includes a Java decompiler framework. It's written in Java, and it can be called as a library. There's not much documentation yet, but I am the author, and I can assist you if you run into trouble--just contact me on BitBucket.
A simple example of how to decompile
java.lang.String
:You can also pass a
.class
file path to thedecompile()
method instead of a class name.If you're not using Java 7, make sure to flush/close your I/O resources manually, e.g.:
After 5 minutes searching on Google, here's where to start:
Especially https://github.com/alexkasko/krakatau-java seems to be pretty easy (from the README on GitHub):