反编译并修复代码后重新编译为*.Jar?
我有一个 myfile.jar 文件。 我使用 jd-gui 反编译 myfile.jar 文件夹中的所有 *.class。我在许多文件夹中得到了许多 *.java 文件
在修复了一些 *.java 中的一些代码之后,现在我想将所有 *.java 重新编译回 *.class,并将整个文件夹打包回 myfile.jar。
我该怎么做?
(这是我第一次玩java代码。)
I have a myfile.jar file.
I use jd-gui to decompile all *.class in a folder in myfile.jar. I get many *.java files in many folders
After fixing some code in some *.java, now I would like to recompile all *.java back to *.class, and pack the whole folder back to myfile.jar.
How do I do this?
(This is my first time playing with java code.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要一个 Java 开发工具包 (JDK)。这包括一个 Java 编译器(通常名为
javac
)和一个 jar 归档器。假设您的 java 文件位于名为
src
的目录中(然后根据它们的包结构),您将使用它来编译所有文件。 (调整*的数量为目录层数。如果你只有一些文件夹,里面有源文件,你也可以单独列出它们。如果某些类依赖于其他类,你可以省略其他类,编译器会找到并列出它们自动编译它们。)
如果程序需要外部库,请为它们提供
-classpath
参数。现在我们已经将所有已编译的类放在目录
classdir
中。查看您的原始 jar 文件:其中的任何非类文件也应该复制到您的 classdir (在与之前相同的相对目录中)。其中最值得注意的是META-INF/MANIFEST.MF
。然后我们从这些创建一个新的 jar 文件。
jar
工具包含在 JDK 中。(您也可以简单地使用您信任的 zip 程序,并将生成的 zip 文件重命名为 .jar。如果您的文件具有非 ASCII 名称,请确保将文件名编码设置为 UTF-8。)
You need a Java Development Kit (JDK). This includes a Java compiler (usually named
javac
), and an jar archiver.Assuming you have the java files in a directory names
src
(then according to their package structure), you would useto compile all files. (Adjust the number of * to the number of directory levels. If you only have some folders with source files in them, you can also list them individually. If some classes depend on others, you can ommit the others, the compiler will find and compile them automatically.)
If the program needs external libraries, provide these with a
-classpath
argument.Now we have all compiled classes in the directory
classdir
. Look into your original jar file: any non-class files in there should also copied into your classdir (in the same relative directory they were before). This most notably includesMETA-INF/MANIFEST.MF
.Then we create a new jar file from these. The
jar
tool is included in the JDK.(You can also simply use a zip program of your confidence and rename the resulting zip file to .jar. If your files have non-ASCII names, make sure to set the filename encoding to UTF-8.)
由于我无法完全重新编译 .jar 文件并使用我的应用程序,因此我想在这里分享之前答案中缺少的步骤。
在我的具体情况下,为了让事情尽可能简单,我只想修改反编译结果中的一个.java文件,并直接在.jar文件中更新它。
我从答案开始,来自Paulo Ebermann,我最终得到:
注意:
path/to/myoriginal.jar
位于依赖项的路径中,这强制 java 编译器在原始 .jar 文件中查找丢失的类,而不是在本地反编译代码中查找丢失的类(这将需要重新编译和更多依赖项),因此我故意省略了.
路径类路径列表-uf
表示 jar 工具应该只更新具有提供的类的文件Since I had trouble getting the .jar file completely recompiled and working with my application, I wanted to share here the steps that were missing in the previous answers.
In my specific case, to make things as easy as possible, I wanted to only modify one .java file in the result of the decompilation and update it directly in the .jar file.
I started from the answer from Paulo Ebermann, and I ended up with:
Notes:
path/to/myoriginal.jar
which is in the path of the dependencies, this forces the java compiler to look for missing classses in the original .jar file rather than in the local decompiled code (which would require recompilation and more dependencies) and therefore I have intentionally left out the.
path in the list of class paths-source 8 -target 8
which were required to force the class to compile for the appropriate version of the runtime machine.-uf
which indicates that the jar tool should only update the file with the provided class一般来说,要将 Java 代码编译成类,您需要 JDK 附带的
javac
可执行文件。Unix:
Windows:
编译完代码后,您可以创建 jar(这是一个 zip 文件,其中包含所有类以及有关创建者、版本等的一些元数据)。
您可以阅读有关可与 javac 一起使用的不同选项的更多信息 和 jar。使用像 ant 这样的工具可以更轻松地编译源代码和创建 jar。请参阅 javac 和 jar 任务。
In General, to compile Java Code into classes you need the
javac
executable that comes with the JDK.Unix:
Windows:
Once you've compiled the code, you can create the jar (which is a zip file that contains all the classes and some meta-data about the creator, version, ...etc).
You can read more about the different options you can use with javac and jar. Using a tool like ant makes it easier to compile source code and create jars. See the javac and the jar tasks.