Java 如何从 Mac 上的 *.jar 文件中提取源代码?

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

How to extract the source code from a *.jar file on a Mac?

javajarextraction

提问by ShanZhengYang

I'm very confused. I downloaded a *.jar file as a bit of software. So, I would like to extract the source code to look at it

我很困惑。我下载了一个 *.jar 文件作为一个软件。所以,我想提取源代码来看看它

I used the command jar xf filename.jar

我使用了命令 jar xf filename.jar

which returned two more *.jarfiles and a *.classfile. I still cannot open these in the terminal with standard text editors.

它返回了另外两个*.jar文件和一个*.class文件。我仍然无法使用标准文本编辑器在终端中打开这些。

Perhaps this is not open source software? Is there an alternative to see what has been done here?

也许这不是开源软件?有没有其他方法可以看看这里做了什么?

采纳答案by Brandon

Run "java -jar fernflower.jar -dgs=true JarToDecompile.jar DecompiledJar"

运行“ java -jar fernflower.jar -dgs=true JarToDecompile.jar DecompiledJar

This is what Intelli-J & Android-Studio Decompiler does.

这就是 Intelli-J 和 Android-Studio Decompiler 所做的。

Note: Fernflower extracts the .javafiles to a .jarfile. You can either Unzip the jar file as a regular zip file (if your version of Archive Utilityon OSX allows it -- It doesn't do it for me on OSX Sierra but works on El Capitan) OR you can do jar xf DecompiledJarand it'll extract it.

注意:Fernflower 将.java文件提取到一个.jar文件中。您可以将 jar 文件解压缩为常规 zip 文件(如果您的Archive UtilityOSX版本允许 - 它在 OSX Sierra 上不适用于我,但适用于 El Capitan),或者您可以这样做jar xf DecompiledJar,它会提取它.

Example (all in one command -- multiple commands separated by &&):

示例(全部在一个命令中 - 多个命令由 分隔&&):

java -jar fernflower.jar -dgs=true JarToDecompile.jar DecompiledJar && cd DecompiledJar && jar xf DecompiledJar.jar && cd ../

java -jar fernflower.jar -dgs=true JarToDecompile.jar DecompiledJar && cd DecompiledJar && jar xf DecompiledJar.jar && cd ../

回答by arcy

A jar file maycontain source code, but more commonly contains only class files. Class files are normally for execution, not for extracting source.

jar 文件可能包含源代码,但更常见的是只包含类文件。类文件通常用于执行,而不是用于提取源代码。

You can decompile class files into source code, but the decompiled code will not be nearly as helpful as the original source code.

您可以将类文件反编译为源代码,但反编译后的代码几乎不会像原始源代码那样有用。

If it is open source, go back to the site where you downloaded the jar files and look for the source. It might be in Zip files to be downloaded, it might be in jar files to be downloaded, and it might be in some kind of repository, like Git, that you can connect to with the right software.

如果它是开源的,请返回下载 jar 文件的站点并查找源。它可能位于要下载的 Zip 文件中,可能位于要下载的 jar 文件中,也可能位于某种存储库中,例如 Git,您可以使用正确的软件连接到它。

回答by saravanan saminathan

Easy solution:

简单的解决方案:

If you have eclipse just add the jar file in the classpath of current project u can see all the packages and source code in the jar. You no need to install and use the commands. You will get a better view of all files

如果你有 Eclipse,只需在当前项目的类路径中添加 jar 文件,你就可以看到 jar 中的所有包和源代码。您无需安装和使用这些命令。您将更好地查看所有文件