java 如何将 jar 文件作为项目导入 eclipse 以使用该 jar 的类的源代码

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

How to import a jar file into eclipse as a project to use source code of classes of that jar

java

提问by Manivasu

I have one jar file. I want to see the source code of classes of this jar file and i need to use source code of this jar file. Is there any option to view and use that jar file source code? Is there any option in eclipse to extract jar file and to see the source code of that jar file?

我有一个 jar 文件。我想查看这个jar文件的类的源代码,我需要使用这个jar文件的源代码。是否有任何选项可以查看和使用该 jar 文件源代码?eclipse中是否有任何选项可以提取jar文件并查看该jar文件的源代码?

回答by wrm

if you add a jar as dependency to your project, you can also link sourcecode to it. Rightclick on project->Build Path->Configure Buildpath. There, choose the jar that you linked into your project, extend it and under "source attachments", you can add the jar that contains the sources of this specific jar

如果您将 jar 作为依赖项添加到您的项目,您还可以将源代码链接到它。右键单击项目->构建路径->配置构建路径。在那里,选择您链接到项目中的 jar,扩展它,在“源附件”下,您可以添加包含此特定 jar 源的 jar

enter image description here

在此处输入图片说明

回答by duffymo

You can add the JAR file with the byte code and attach the source JAR as well. You can't do without the first; the second is optional.

您可以添加带有字节码的 JAR 文件并附加源 JAR。你不能没有第一个;第二个是可选的。

It's easy to do in IntelliJ; I'm not sure about the steps in Eclipse.

在 IntelliJ 中很容易做到;我不确定 Eclipse 中的步骤。

回答by Dale Cooper

The JAR file you are refering too may not have source code attached. Usually, when downloading third party library you can download JARs with byte-code and source code separetredly. The best way to check is to simply extract JAR file (with ZIP extracting tool) and check whether you have source there. If so, you can do the following: 1. Add the JAR to your Eclipse project (just drag it there and choose whether you want to copy or link to the JAR). 2. Whenever you are opening class file that does not come with source-code and you are shown "Source not found" page - just press button "Attach Source.." and find your JAR file with source code.

您也引用的 JAR 文件可能没有附加源代码。通常,在下载第三方库时,您可以分别下载字节码和源代码的 JAR。检查的最佳方法是简单地提取 JAR 文件(使用 ZIP 提取工具)并检查您是否有源文件。如果是这样,您可以执行以下操作: 1. 将 JAR 添加到您的 Eclipse 项目(只需将它拖到那里并选择是要复制还是链接到 JAR)。2. 当你打开没有源代码的类文件时,你会看到“找不到源”页面 - 只需按下按钮“附加源..”并找到带有源代码的 JAR 文件。

[EDIT] use JD-GUI to extract source code (*.java files) from *.class files contained in JAR file.

[编辑] 使用JD-GUI从包含在 JAR 文件中的 *.class 文件中提取源代码(*.java 文件)。

回答by Marcin Szymczak

Right click on Your procjet. Chose properties.

右键单击您的 procjet。选择属性。

Select Libraries enter image description hereChose Your jar enter image description hereProfit enter image description here

选择图书馆 在此处输入图片说明选择您的罐子 在此处输入图片说明利润 在此处输入图片说明

回答by user2995311

In my case I had in Java Build Path Source attachment with meaningful path (not NONE), but file itself was absent in this place. I configure project to Maven nature. And then from Maven make Download sources - file -source.jar appears.

在我的情况下,我在 Java Build Path Source 附件中有有意义的路径(不是 NONE),但文件本身在这个地方不存在。我将项目配置为 Maven 性质。然后从 Maven make Download sources - file -source.jar 出现。

回答by ashish gupta

Yes,You can see the source code of all the files those are added in your jar file.first you have to add jar file same as shown by the Marcin Szymczak. and after that click on "Referenced library". After that click on Your jar.and you can check all the classes source code.In my case jar file-Hackerrank.jar and you can see source code of classes:

是的,您可以看到添加到 jar 文件中的所有文件的源代码。首先,您必须添加与 Marcin Szymczak 所示相同的 jar 文件。然后点击“参考库”。然后点击你的 jar.and 你可以检查所有类的源代码。在我的例子中是 jar 文件-Hackerrank.jar,你可以看到类的源代码:

enter image description here

在此处输入图片说明

回答by Kaba Aboubacar

This applies to plugin jars (if that is your case):

这适用于插件 jars(如果是你的情况):

  1. Ensure to put the jar into your target platform folder

  2. Refresh your target platform: Window -> Preferences -> Target Platform -> Select your current target -> Edit -> Finish -> Reload -> Ok

  3. Open the "Plug-ins" view: Window -> Show view -> Plug-ins

  4. In that view, locate the new plugin jar: right click and Import As -> Source Project.

  1. 确保将 jar 放入目标平台文件夹

  2. 刷新你的目标平台:Window -> Preferences -> Target Platform -> Select your current target -> Edit -> Finish -> Reload -> Ok

  3. 打开“插件”视图:窗口 -> 显示视图 -> 插件

  4. 在该视图中,找到新的插件 jar:右键单击并导入为 -> 源项目。

You will have it as a plugin project in your workspace.

您将在工作区中将其作为插件项目使用。