Java类反编译和重新编译

发布于 2024-07-24 04:45:43 字数 393 浏览 7 评论 0原文

我有一个程序,其中有一些 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 without animal.class?
  • Can I recompile dog.java without animal.java?

I am not a Java developer, so please correct me if I'm not making any sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

自此以后,行同陌路 2024-07-31 04:45:43

您需要 class 文件才能实际使用该类,但这并不意味着您需要源文件。

让我们看看使用 DogAnimal 类示例的可能性。 假设 Dog 类是 Animal 类的子类,我们可以执行以下操作:

  • 如果您同时拥有 Animal.classAnimal.java,您可以创建一个 Dog 类。

  • 如果您有 Animal.class 但没有 Animal.java,您可以创建一个 Dog 类。

  • 如果您没有 Animal.class 但有 Animal.java,您可以创建一个 Dog 类。 (这意味着在编译 Dog.java 文件时,需要编译 Animal.java 文件。)

  • 如果您没有 >Animal.classAnimal.java,您无法创建 Dog 类。

以下是上述内容的表格版本:

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

如果您有 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 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.

甜中书 2024-07-31 04:45:43

一般来说,没有。 但是,您可以为那些缺少的类创建存根(仅用于编译),然后在拥有它们时替换原始类。

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.

星星的轨迹 2024-07-31 04:45:43

如果您有权访问已编译的类(示例中的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...

和我恋爱吧 2024-07-31 04:45:43

只是为了添加其他答案,您可能需要注意的一个方面是,如果您的类具有复杂的结构,反编译器可能无法将其反编译为“可重新编译”状态,您可能需要手动编辑它。

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.

梓梦 2024-07-31 04:45:43

如果您可以运行该类并反编译它,那么您就有了编译修改后的 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.

静谧幽蓝 2024-07-31 04:45:43

我的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文