Java 如何编译 .class 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24142367/
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-14 10:26:59  来源:igfitidea点击:

How to compile .class files

javaeclipse.class-file

提问by Moon

I am new to Java, I have some *.class files which needs to be modified. Now I de-compiled these *.class files to get the source code using the famous JD Decompiler. The concern is after I change the code and need to recompile these *.class files how will I do it with Eclipse. I only the compiled *.class files.

我是 Java 新手,我有一些 *.class 文件需要修改。现在我使用著名的JD Decompiler 反编译这些 *.class 文件以获取源代码。问题是在我更改代码并需要重新编译这些 *.class 文件之后,我将如何使用 Eclipse 来完成。我只编译了 *.class 文件。

I know its a stupid question but bear with me I'm .NET Developer with a few days into Java.

我知道这是一个愚蠢的问题,但请耐心等待我是 .NET 开发人员,对 Java 有几天的经验。

采纳答案by Shail016

You can not compile a *.classfile, Its already compiled code (bytecode) for its *.javasource. I think you need to copy the code from JD Decompiler's editor to a java source file.

你不能编译一个*.class文件,它的*.java源代码是已经编译好的代码(字节码)。我认为您需要将代码从 JD Decompiler 的编辑器复制到 Java 源文件中。

i.e. copy the source code from JD's editor for Hello.classinto a file named Hello.java. Now you can move this file in IDE of your choice or directly compile using javac. (dont forget to put all the dependencies/required jars/source in project build path)

即将源代码从京东的编辑器复制Hello.class到一个名为Hello.java. 现在您可以在您选择的 IDE 中移动此文件或直接使用 javac 进行编译。(不要忘记将所有依赖项/必需的 jars/source 放在项目构建路径中)

回答by PabloTG

I think that you can use javac.

我认为您可以使用 javac。

For example, if you have three .javafiles:

例如,如果您有三个.java文件:

source1.java
directory\source2.java
directory\directory2\source3.java

source1.java
目录\source2.java
目录\directory2\source3.java

Execute:

执行:

javac source1.java directory\source2.java package\package2\source3.java

And you can create a file.jar with all .class:

您可以创建一个包含所有 .class 的 file.jar:

jar cf file.jar source1.class directory\source2.class directory\directory2\source3.class