简体   繁体   English

附上jar的Eclipse中的源码

[英]Attach the Source in Eclipse of a jar

I added a Student.jar into my Build Path in my eclipse like this-我在 eclipse 的Build Path中添加了一个Student.jar ,就像这样-

Right click on the project->BuildPath->Configure Build Path->Libraries->Add External Jars右击项目->BuildPath->Configure Build Path->Libraries->Add External Jars

There is one class named StudentTest in Student.jar file. Student.jar文件中有一个名为StudentTest的class。 When I was debugging my code in eclipse, I stepped into that StudentTest class in the Student.jar .当我在 eclipse 中调试我的代码时,我进入了Student.jar中的StudentTest class 。

And after that eclipse shows me like this-在那之后 eclipse 向我展示了这样的 -

The JAR file S:\some_location\Student.jar has no source attachment. JAR文件S:\some_location\Student.jar没有源码附件。 You can attach the source by clicking Attach Source below您可以通过单击下面的附加源来附加源

Now I am not sure how should I attach the source in my eclipse. And from where?现在我不确定我应该如何在我的 eclipse 中附加源代码。从哪里? Can anyone provide me step by step what I need to do.任何人都可以逐步向我提供我需要做的事情。

Update:- I tried unzipping the Student.jar and I got Student folder .更新:-我尝试解压缩Student.jar并且我得到了Student folder And after that I tried pointing the source to Student folder But still I am not able to see the class properly so that I can debug it properly, it shows the same above behavior.之后我尝试将源指向Student folder但我仍然无法正确看到 class 以便我可以正确调试它,它显示了与上述相同的行为。

Use Java Source Attacher !使用Java 源附加程序

It does what eclipse should do - a right click context menu that says "Attach Java Source.它完成了 eclipse 应该做的事情——一个右键单击上下文菜单,上面写着“附加 Java 源代码。

在此处输入图片说明

It automatically downloads the source for you and attaches it.它会自动为您下载源代码并附加它。 I've only hit a couple libraries it doesn't know about and when that happens it lets you contribute the url back to the community so no one else will have a problem with that library.我只访问了几个它不知道的库,当发生这种情况时,它可以让您将 url 贡献回社区,这样其他人就不会对该库有问题。

Eclipse is showing no source found because there is no source available . Eclipse 显示找不到源,因为没有可用的源。 Your jar only has the compiled classes.您的 jar 只有已编译的类。

You need to import the project from jar and add the Project as dependency .您需要从 jar 导入项目并将项目添加为依赖项。

Other option is to go to the另一种选择是去

Go to Properties (for the Project) -> Java Build Path -> Libraries , select your jar file and click on the source , there will be option to attach the source and Javadocs.转到 Properties (for the Project) -> Java Build Path -> Libraries ,选择你的 jar 文件并点击 source ,会有附加源和 Javadocs 的选项。

A .jar file usually only contains the .class files, not the .java files they were compiled from. .jar 文件通常只包含 .class 文件,而不包含编译它们的 .java 文件。 That's why eclipse is telling you it doesn't know the source code of that class.这就是 eclipse 告诉您它不知道该类的源代码的原因。

"Attaching" the source to a JAR means telling eclipse where the source code can be found.将源代码“附加”到 JAR 意味着告诉 eclipse 可以找到源代码的位置。 Of course, if you don't know yourself, that feature is of little help.当然,如果您不了解自己,那么该功能将无济于事。 Of course, you could try googling for the source code (or check wherever you got the JAR file from).当然,您可以尝试使用谷歌搜索源代码(或检查您从何处获得 JAR 文件)。

That said, you don't necessarily need the source to debug.也就是说,您不一定需要调试的源代码。

This worked for me for Eclipse-Luna:这对 Eclipse-Luna 对我有用:

  1. Right Click on the *.jar in the Referenced Libraries folder under your project, then click on Properties右键单击项目下 Referenced Libraries 文件夹中的 *.jar,然后单击 Properties
  2. Use the Java Source Attachment page to point to the Workspace location or the External location to the source code of that jar.使用 Java 源附件页面指向工作区位置或指向该 jar 源代码的外部位置。

Go back in to where you added the jar.返回到添加 jar 的位置。 I believe its the libraries tab, I don't have Eclipse open but that sounds right.我相信它的库选项卡,我没有打开 Eclipse,但这听起来不错。 to the left of the jar file you added there should be an arrow pointing right, click that and 3 or 4 options expand, one of them being the source file of the library.在您添加的 jar 文件的左侧应该有一个指向右侧的箭头,单击该箭头并展开 3 或 4 个选项,其中一个是库的源文件。 Click on that and click edit(I think you can also double click it) then locate the file or folder on your hard disk, you probably have to click apply or okay and you're good to go, same with javadoc and i think the last one is native libraries.单击它并单击编辑(我认为您也可以双击它)然后在硬盘上找到文件或文件夹,您可能必须单击应用或好的,然后就可以了,与 javadoc 相同,我认为最后一个是本地库。 I don't pay much attention when I'm in there anymore if you couldn't tell.如果你不知道,我在那里的时候就不会太注意了。 That's what you were asking, right?你问的是这个吧?

I Know it is pretty late but it will be helpful for the other user, as we can do Job using three ways... as below我知道现在已经很晚了,但它对其他用户会有所帮助,因为我们可以使用三种方式来完成工作......如下
1)1. 1)1。 Atttach your source code using使用附加源代码
ie, Right click on the project then properties --> Java build path--> attach your source in the source tab or you can remove jar file and attach the source in the libraries tab即,右键单击项目然后属性--> Java 构建路径--> 在源选项卡中附加您的源,或者您可以删除 jar 文件并在库选项卡中附加源
2. Using eclipse source Analyzer 2.使用eclipse源码分析器
In the eclipse market you can download the plugin java source analyzer which is used to attach the open source jar file's source code.在eclipse市场可以下载插件java源分析器,用于附加开源jar文件的源代码。 we can achieve it after installing the plugin, by right click on the open source jar and select the attach source option.我们可以在安装插件后通过右键单击开源jar并选择附加源选项来实现它。
3. Using Jadclipse in eclipse you can do it 3.在eclipse中使用Jdclipse就可以了
last not the least, you can achieve the decompile your code using this plugin.最后一点,您可以使用此插件实现反编译您的代码。 it is similar way you can download the plugin from the eclipse market place and install in your eclipse.您可以通过类似的方式从 eclipse 市场下载插件并安装在您的 eclipse 中。 in jadclipse view you can see your .class file to decomplile source format note here you cannot see the comment and hidden things在 jadclipse 视图中,你可以看到你的 .class 文件来反编译源格式注意这里你看不到注释和隐藏的东西

I think in your scenario you can use the option one and option three, I prefer option three only if i want to the source code not for the debug the code.我认为在您的场景中,您可以使用选项一和选项三,只有当我想要源代码而不是调试代码时,我才更喜欢选项三。 else i ll code the option 1, as i have the source already available with.否则我将对选项 1 进行编码,因为我已经有了可用的源代码。

Simply import the package of the required source class in your code from jar.只需从 jar 中导入代码中所需源类的包。

You can find jar's sub packages in你可以在里面找到jar的子包

Eclipse -- YourProject --> Referenced libraries --> yourJars --> Packages --> Clases Eclipse -- YourProject --> 引用的库 --> yourJars --> 包 --> 类

Like-- I was troubling with the mysql connector jar issue "the source attachment does not contain the source" by giving the path of source folder it display this statement就像 - 我对mysql connector jar问题“源附件不包含源”感到困扰,因为它提供了显示此语句的源文件夹的路径

The source attachment does not contain the source for the file StatementImpl.class

Then I just import the package of mysql connector jar which contain the required class:然后我只需导入包含所需类的mysql connector jar包:

import com.mysql.jdbc.*;

Then program is working fine.然后程序运行良好。

It is quite possible, just go to the jar in the Build Path and choose to attach a source just like follow.很有可能,只需在构建路径中将 go 改成 jar 并选择附加源即可,如下所示。

在此处输入图像描述

I faced the same issue and solved using the below steps.我遇到了同样的问题并使用以下步骤解决了。 Go to Windows->preferences->Editors->File Associations转到 Windows-> 首选项-> 编辑器-> 文件关联

Here click on Add then type .class click on OK在这里点击添加然后输入 .class 点击确定

again click on Add then type .classwithoughtsource click on OK再次点击添加然后输入 .classwithoughtsource 点击确定

Now you will be able to see JadClipse option under Java section in Windows->Preferences现在您将能够在 Windows->Preferences 的 Java 部分下看到 JadClipse 选项

Please provide the path of jad.exe file as shown below.请提供 jad.exe 文件的路径,如下所示。

Path for Decompiler-C:\\Users\\ahr\\Documents\\eclipse-jee-galileo-SR2-win32\\jad.exe Directory for temporary Files-C:\\Users\\ahr.net.sf.jadclipse反编译器路径-C:\\Users\\ahr\\Documents\\eclipse-jee-galileo-SR2-win32\\jad.exe 临时文件目录-C:\\Users\\ahr.net.sf.jadclipse

click on Apply点击应用

Now you should be able to see the classfiles in proper format.现在您应该能够以正确的格式查看类文件。

  1. Download JDEclipse from http://java-decompiler.github.io/http://java-decompiler.github.io/下载 JDEclipse
  2. Follow the installation steps按照安装步骤
  3. If you still didn't find the source, right click on the jar file and select " Attach Library Source " option from the project folder, as you can see below.如果您仍然没有找到源代码,请右键单击 jar 文件并从项目文件夹中选择“附加库源”选项,如下所示。

下拉选项位置

I have faced same problem and resolved it by using following scenario.我遇到了同样的问题并通过使用以下场景解决了它。

  1. First we have to determine which jar file's source code we want along with version number.首先,我们必须确定我们想要哪个 jar 文件的源代码以及版本号。 For Example "Spring Core" » "4.0.6.RELEASE"例如“Spring Core”»“4.0.6.RELEASE”

  2. open https://mvnrepository.com/ and search file with name "Spring Core" » "4.0.6.RELEASE".打开https://mvnrepository.com/并搜索名为“Spring Core”»“4.0.6.RELEASE”的文件。

  3. Now Maven repository will show the the details of that jar file.现在 Maven 存储库将显示该 jar 文件的详细信息。

  4. In that details there is one option "View All" just click on that.在该详细信息中有一个选项“查看全部”,只需单击该选项即可。

  5. Then we will navigate to URL "https://repo1.maven.org/maven2/org/springframework/spring-core/4.0.6.RELEASE/".然后我们将导航到 URL“https://repo1.maven.org/maven2/org/springframework/spring-core/4.0.6.RELEASE/”。

  6. there so many options so select and download "spring-core-4.0.6.RELEASE-sources.jar" in our our system and attach same jar file as a source attachment in eclipse.有很多选项,所以在我们的系统中选择并下载“spring-core-4.0.6.RELEASE-sources.jar”,并在eclipse中附加相同的jar文件作为源附件。

I am using project is not Spring or spring boot based application.我使用的项目不是基于 Spring 或 Spring Boot 的应用程序。 I have multiple subprojects and they are nested one within another.我有多个子项目,它们相互嵌套。 The answers shown here supports on first level of subproject.此处显示的答案支持子项目的第一级。 If I added another sub project for source code attachement, it is not allowing me saying folder already exists error.如果我为源代码附件添加了另一个子项目,则不允许我说文件夹已存在错误。

Looks like eclipse is out dated IDE.看起来 eclipse 是过时的 IDE。 I am using the latest version of Eclipse version 2015-2019.我正在使用最新版本的 Eclipse 版本 2015-2019。 It is killing all my time.它正在杀死我所有的时间。

My intension is run the application in debug mode navigate through the sub projects which are added as external dependencies (non modifiable).我的意图是在调试模式下运行应用程序,浏览作为外部依赖项(不可修改)添加的子项目。

Try removing the breakpoints.尝试删除断点。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM