views:

204

answers:

6

I have a program where some java classes are available. It is possible to decompile them. Is it possible to modify the source code of a class and recompile it, without having all the other .class ?

For example, suppose I have a dog.class file, that implement a subclass of animal, which is defined in animal.class. - Can I recompile dog.java without animal.class ? - Can I recompile dog.java without animal.java ?

I am not a java developper, so please correct me if What I am writing here is non sense.

+1  A: 

In general, no. You could, however, make stubs for those missing classes (just for the compile), and then substitute in the original when you do have them.

Chaos
+1  A: 

If you have access to a compiled class (animal.class in your example) then you can recompile a subclass (dog.java) (you don't, however, need to have access to the source code of the superclass).

If you don't even have access to the compiled superclass then if you know the relevant methods (those which are implemented or called from the subclass) then you can create a stub of the superclass just to get the subclass to compile.

At some point, however, you will need a real version of the compiled superclass to allow your code to execute correctly...

Matthew Murdoch
+2  A: 

You'll need the class file in order to actually use the class, but that doesn't mean you'll need the source file.

Let's take a look at the possibilities using the Dog and Animal class example. Assuming that the Dog class is a subclass of the Animal class, we can do the following:

  • If you have both Animal.class and Animal.java, you can make a Dog class.

  • If you have Animal.class but not Animal.java, you can make a Dog class.

  • If you don't have Animal.class but have Animal.java, you can make a Dog class. (This means that the Animal.java file will need to be compiled when the Dog.java file is compiled.)

  • If you don't have either Animal.class nor Animal.java, you cannot make a Dog class.

Here is a tabular version of the above:

 Have Animal.java?     Have Animal.class?       Can make Dog class?
 -----------------     ------------------       -------------------
        Yes                    Yes          -->          Yes
        Yes                    No           -->          Yes
        No                     Yes          -->          Yes
        No                     No           -->          No

If you have the class file, there are programs which can decompile the class file to produce a human-readable java source file, however, it will not restore the exact source that was used to produce the class file.

However, in this case, if all that is necessary is to extend the Animal class to make a new Dog class, the source code itself is not necessary.

Keep this in mind -- whenever a class is made in Java, it always extends the Object class. Even if we don't have access to the source code of the Object class, since we have access to the Object.class in Java, we are able to create our own classes.

This case is similar -- as long as we have the class file for a class, we can use it to the fullest extent. The only thing that is missing is the actual implementation which is listed in the source code.

coobird
A: 

Just to add to the other answers, one aspect you might have to watch out for is that if your class has complex structures, the decompiler might not be able to decompile it to a 'recompilable' state and you might need to edit it manually.

talonx
A: 

If you can run the class, and decompile it, then you have the necessary classes to compile the revised Java file (if you include them on your classpath).

Note that your new classfile must be in "front" of the original classes on the classpath to take effect.

Also note that "sealed jarfiles" does not allow this trick to modify classes.

Thorbjørn Ravn Andersen
You don't need other classes to decompile.
shodanex
A: 

My 2 cents: Some decompliers prevent at physical level classes from decompiling (e.g. you'll get crash during decompiling process on some decompilers). Additional issue that I met with decompilers, only few of them supports annotations. In my case, when I try to decompile a class I've got a deadlock: code contains annotation, but can be decompiled w/o crash only by decompiler, that didn't support annotation.

FoxyBOA