Java类反编译和重新编译
我有一个程序,其中有一些 Java 类可用。 可以反编译它们吗? 是否可以修改类的源代码并重新编译它,而不需要所有其他 .class
文件?
例如,假设我有一个 dog.class
文件,它实现了 animal.class
中定义的animal 子类。
- 我可以在没有
animal.class
的情况下重新编译dog.java
吗? - 我可以在没有
animal.java
的情况下重新编译dog.java
吗?
我不是Java开发人员,所以如果我没有任何意义,请纠正我。
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
files?
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
withoutanimal.class
? - Can I recompile
dog.java
withoutanimal.java
?
I am not a Java developer, so please correct me if I'm not making any sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要
class
文件才能实际使用该类,但这并不意味着您需要源文件。让我们看看使用
Dog
和Animal
类示例的可能性。 假设Dog
类是Animal
类的子类,我们可以执行以下操作:如果您同时拥有
Animal.class
和Animal.java
,您可以创建一个Dog
类。如果您有
Animal.class
但没有Animal.java
,您可以创建一个Dog
类。如果您没有
Animal.class
但有Animal.java
,您可以创建一个Dog
类。 (这意味着在编译Dog.java
文件时,需要编译Animal.java
文件。)如果您没有
>Animal.class
或Animal.java
,您无法创建Dog
类。以下是上述内容的表格版本:
如果您有
class
文件,则有一些程序可以反编译class
文件以生成人类可读的java< /code> 源文件,但是,它不会恢复用于生成
class
文件的确切源。但是,在这种情况下,如果只需扩展
Animal
类来创建一个新的Dog
类,则源代码本身就不是必需的。请记住这一点——每当用 Java 创建一个类时,它总是扩展
Object
类。 即使我们无法访问Object
类的源代码,由于我们可以访问 Java 中的Object.class
,我们也可以创建自己的类。这种情况也类似——只要我们有一个类的
class
文件,我们就可以最大限度地使用它。 唯一缺少的是源代码中列出的实际实现。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
andAnimal
class example. Assuming that theDog
class is a subclass of theAnimal
class, we can do the following:If you have both
Animal.class
andAnimal.java
, you can make aDog
class.If you have
Animal.class
but notAnimal.java
, you can make aDog
class.If you don't have
Animal.class
but haveAnimal.java
, you can make aDog
class. (This means that theAnimal.java
file will need to be compiled when theDog.java
file is compiled.)If you don't have either
Animal.class
norAnimal.java
, you cannot make aDog
class.Here is a tabular version of the above:
If you have the
class
file, there are programs which can decompile theclass
file to produce a human-readablejava
source file, however, it will not restore the exact source that was used to produce theclass
file.However, in this case, if all that is necessary is to extend the
Animal
class to make a newDog
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 theObject
class, since we have access to theObject.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.一般来说,没有。 但是,您可以为那些缺少的类创建存根(仅用于编译),然后在拥有它们时替换原始类。
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.
如果您有权访问已编译的类(示例中的animal.class),那么您可以重新编译子类(dog.java)(但是,您不需要访问源代码超类)。
如果您甚至无法访问已编译的超类,那么如果您知道相关方法(从子类实现或调用的方法),那么您可以创建超类的存根以使子类进行编译。
然而,在某些时候,您将需要编译超类的真实版本,以允许您的代码正确执行......
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...
只是为了添加其他答案,您可能需要注意的一个方面是,如果您的类具有复杂的结构,反编译器可能无法将其反编译为“可重新编译”状态,您可能需要手动编辑它。
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.
如果您可以运行该类并反编译它,那么您就有了编译修改后的 Java 文件所需的类(如果您将它们包含在类路径中)。
请注意,新的类文件必须位于类路径上原始类的“前面”才能生效。
另请注意,“密封的 jarfiles”不允许此技巧修改类。
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.
我的2美分:一些反编译器会在物理级别阻止类反编译(例如,在某些反编译器上的反编译过程中,您会崩溃)。 我在反编译器中遇到的其他问题是,只有少数反编译器支持注释。 就我而言,当我尝试反编译一个类时,我遇到了死锁:代码包含注释,但只能通过 反编译器,不支持注释。
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.