简体   繁体   English

编译或不编译.class文件

[英]Compile or not to compile .class files

There is a web application, that is being ran on Windows, inside a Tomcat browser. 在Tomcat浏览器中有一个在Windows上运行的Web应用程序。 Application users number of .jar files as a storage for it's logic. 应用程序用户数.jar文件作为其逻辑存储。

When I open a jar file, that I need to alter a logic of, I see a bunch of .class files. 当我打开一个jar文件时,需要更改其逻辑,我看到了一堆.class文件。 (Java compiled classes, I assume). (我假设是Java编译的类)。 When I try to open .class file in a text editor, I see a semi-readable gibberish. 当我尝试在文本编辑器中打开.class文件时,看到半可读的乱码。

When I feed this compiled .class to a service like showmycode.com, I can see the actual java code. 当我将此编译后的.class提供给showmycode.com之类的服务时,我可以看到实际的Java代码。 At this point I alter the code according to my need and want to put the updated .class file for application to use (put it back inside a jar). 此时,我根据需要更改代码,并希望将更新的.class文件放入应用程序中使用(将其放回jar中)。

Is it mandatory that I compile the .class file to a "semi-readable gibberish", or I can put it there as a source code file (probably not, but thought I would ask just in case). 我是否必须将.class文件编译为“半可读的乱码”,或者可以将其作为源代码文件放在那里(可能不是,但是以为以防万一)。 If I have to compile the atered code, can I do it without bringing up a project in some Java IDE, and just compile it into .class via command line or something? 如果必须编译经过验证的代码,是否可以在不通过Java IDE调用项目的情况下进行操作,而仅通过命令行或其他方式将其编译为.class?

There are two main methods for editing an application you don't have the source for. 编辑您没有源的应用程序有两种主要方法。

The first one, as you've discovered is decompile, edit, recompile. 您已经发现,第一个是反编译,编辑,重新编译。 The advantage is that you don't have to know anything about bytecode since it's just decompiled to Java. 好处是您无需了解任何字节码,因为它只是反编译为Java。 The main disadvantage is that even the best decompilers can't guarantee producing recompileable code. 主要缺点是,即使是最好的反编译器也无法保证产生可重新编译的代码。 And if the application has been obfuscated at all, you can forget about recompilation. 而且,如果应用程序完全被混淆了,您可以忘记重新编译。

The alternative is disassemble, edit, reassemble. 替代方法是反汇编,编辑,重新组装。 I'd recommend Krakatau for this (disclosure, I wrote it). 我会为此推荐Krakatau (公开,我写了它)。 One alternative is ASM, but it's designed for programatic modification, not hand editing, and it's clunkier. 一种替代方法是ASM,但它是为程序修改而不是手动编辑而设计的,而且笨重。

The main advantage of bytecode editing is that it always works, even if the application has been obfuscated. 字节码编辑的主要优点是,即使对应用程序进行了混淆,它也始终可以正常工作。 The downside is that it obviously requires you to understand and be able to program directly in bytecode. 缺点是,它显然需要您理解并能够直接使用字节码进行编程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM