Java 重新编译为*.Jar 反编译修复后的代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6604097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:07:57  来源:igfitidea点击:

Recompile to *.Jar After decompile and fix the code?

javajarjavac

提问by sandy

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

我有一个 myfile.jar 文件。我使用 jd-gui 反编译 myfile.jar 文件夹中的所有 *.class。我在许多文件夹中得到许多 *.java 文件

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.

在某些*.java 中修复了一些代码后,现在我想将所有*.java 重新编译回*.class,并将整个文件夹打包回myfile.jar。

How do I do this?

我该怎么做呢?

(This is my first time playing with java code.)

(这是我第一次玩java代码。)

回答by Moe Matar

In General, to compile Java Code into classes you need the javacexecutable that comes with the JDK.

一般来说,要将 Java 代码编译成类,您需要javacJDK 附带的可执行文件。

Unix:

Unix:

${JAVA_HOME}/bin/javac -d OUTPUT_DIRECTORY SOURCE_FILES

Windows:

视窗:

%JAVA_HOME%\bin\javac.exe -d OUTPUT_DIRECTORY SOURCE_FILES

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).

编译代码后,您可以创建 jar(这是一个 zip 文件,其中包含所有类和一些有关创建者、版本等的元数据)。

${JAVA_HOME}/bin/jar cf output.jar INPUT_FILES

You can read more about the different options you can use with javacand jar. Using a tool like ant makes it easier to compile source code and create jars. See the javacand the jartasks.

您可以阅读有关可以与javacjar一起使用的不同选项的更多信息。使用 ant 之类的工具可以更轻松地编译源代码和创建 jar。请参阅javacjar任务。

回答by Pa?lo Ebermann

You need a Java Development Kit (JDK). This includes a Java compiler (usually named javac), and an jar archiver.

您需要一个 Java 开发工具包 (JDK)。这包括一个 Java 编译器(通常命名为javac)和一个 jar 归档器。

Assuming you have the java files in a directory names src(then according to their package structure), you would use

假设您在目录名称中有 java 文件src(然后根据它们的包结构),您将使用

javac -d classdir -sourcepath src src/*.java src/*/*.java src/*/*/*.java ...

to 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 -classpathargument.

如果程序需要外部库,请为它们提供-classpath参数。

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 includes META-INF/MANIFEST.MF.

现在我们在目录中拥有所有已编译的类classdir。查看您的原始 jar 文件:其中的任何非类文件也应复制到您的类目录中(在它们之前所在的相同相对目录中)。这最显着的包括META-INF/MANIFEST.MF.

Then we create a new jar file from these. The jartool is included in the JDK.

然后我们从这些创建一个新的 jar 文件。该jar工具包含在 JDK 中。

jar cfm mypackage.jar classdir/META-INF/MANIFEST.MF -C classdir .

(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.)

(您也可以简单地使用您信任的 zip 程序并将生成的 zip 文件重命名为 .jar。如果您的文件具有非 ASCII 名称,请确保将文件名编码设置为 UTF-8。)