1

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.

Daanish
  • 1,061
  • 7
  • 18
  • 29
  • And why would want to do something like this? – Snake Eye Nov 08 '12 at 05:02
  • 1
    You 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 Answers3

1

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!

Community
  • 1
  • 1
Vineet Kosaraju
  • 5,572
  • 2
  • 19
  • 21
1

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

Friso
  • 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
0

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

AZetZ
  • 21
  • 3