Is it possible to decompile a java .class file within another program ? I'm aware of tools like CAVAJ and DJ Java Decompiler. Why ? : I'm modifying a class file within the program and dumping it into the same. I want to then parse the source file of the modified class file. Thanks in advance.
-
And why would want to do something like this? – Snake Eye Nov 08 '12 at 05:02
-
1You might be able to execute the process of running the decompiler tool (I use JD-GUI) and then edit the code and recompile? I think it is "Runtime.exec()"? – Vineet Kosaraju Nov 08 '12 at 05:09
-
@SnakeEye I'm instrumenting the class file. After instrumenting it I want to parse the instrumented file(which I will get by decompiling the class file) in the same program. – Daanish Nov 09 '12 at 08:43
-
Thank you for your answer @Bucco .I'd appreciate it if you could elaborate on how I could use a GUI decompiler in `Runtime.exec()`. – Daanish Nov 09 '12 at 08:45
3 Answers
Here is a possible answer (this is what I use for compiling source files)
private int runProcess(String command) throws Exception
{
Process pro = Runtime.getRuntime().exec(command);
System.out.println(command);
printLines("", pro.getInputStream());
printLines("",pro.getErrorStream());
pro.waitFor();
return(pro.exitValue());
}
Then you can use something like:
int exitValue = runProcess(your command here);
I guess JD-GUI will not work; you need a command line java decompiler.
These links might help:
Batch decompiling of Java files with JD-GUI
http://en.wikipedia.org/wiki/JAD_%28JAva_Decompiler%29
http://viralpatel.net/blogs/decompile-class-file-java-decompiler-class-java-class/
The last link has a download link to JAD, a command line tool for windows computers.
Depending on the OS the command might vary but it might just be "jad classname.class" if you move jad to your cmd class path. I have mac os x so I am not sure where this is located.
I hope this helps!

- 1
- 1

- 5,572
- 2
- 19
- 21
-
Thank you for your answer. The problem is all the decompilers I can find are GUI based. – Daanish Nov 22 '12 at 04:58
If you're looking to modify the class at runtime it may not be needed to decompile the class. You can manipulate the bytecode. Take a look at http://cglib.sourceforge.net/, maybe that'll help you

- 1,080
- 6
- 37
-
you are right..I am able to modify the bytecode. I want to then decompile the modified bytecode to do some other manipulations. – Daanish Nov 19 '12 at 04:59
Java IDE works fine to decompile class files. Try IntelliJ as an example, even Community Edition. It is not able to update (read only mode) but I hope it can help

- 21
- 3