Attach the Source in Eclipse of a jar
Asked Answered
V

12

46

I added a Student.jar into my Build Path in my eclipse like this-

Right click on the project->BuildPath->Configure Build Path->Libraries->Add External Jars

There is one class named StudentTest in Student.jar file. When I was debugging my code in eclipse, I stepped into that StudentTest class in the Student.jar.

And after that eclipse shows me like this-

The JAR file S:\some_location\Student.jar has no source attachment. 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? Can anyone provide me step by step what I need to do.

Update:- I tried unzipping the Student.jar and I got 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.

Vickery answered 2/3, 2013 at 22:56 Comment(8)
@Kata Please explain how the link you give will answer the question.Hastate
@Kata, I am confused on this line ` Fill in the Location path field depending on the location, choose between the workspace, an external file or external folder. Click OK. ` What location path I need to choose. Should I unzip the Student.jar folder then point to Student folder? Or something else. As I tried the above option, and I am still seeing the same behavior.Vickery
@Nevzz03 I am sorry, I pasted the wrong link. Here is the right one: help.eclipse.org/indigo/…Bicephalous
@Kata, see my previous comment.Vickery
@Nevzz03 You need to point to the location of the source files, not the compiled class files. If your JAR file contains .java files, then you should be able to point to it directly. Unless you explicitly added these yourself, I doubt this is the case, so unzipping the JAR file won't be helpful anyway.Hastate
related #8455090Ameeameer
related #371314Ameeameer
@AdrienBe I followed instruction per your 1st link and Open declaration takes me to .class file instead of .java. Can you please clarify this scenario ?Dogtired
V
16

A .jar file usually only contains the .class files, not the .java files they were compiled from. That's why eclipse is telling you it doesn't know the source code of that class.

"Attaching" the source to a JAR means telling eclipse where the source code can be found. 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).

That said, you don't necessarily need the source to debug.

Visit answered 2/3, 2013 at 23:6 Comment(8)
Thanks for the suggestion. After unzipping the jar I will be getting a folder right? So if I point to that folder, I should be able to see the source code properly right?Vickery
Only if the JAR file contains the source code (the *.java files), which most JAR files don't ...Visit
Yeah, you are right. After unzipping the jar files, I found only .class files there. So there is no way I can make this thing work right?Vickery
@Nevzz03 Do you have the .java files that were compiled into those .class files? If not, then no, you cannot attach the source files to Eclipse because you don't have them.Hastate
If you have the jar file with only .class files, you don't have the source code (the .java files). BUT you can generate it using the appropriate tool, ie. jd-gui . In jd-gui, open your jar, then go to File > Save all Source, trick is done :) see #8455090Ameeameer
Another possibility if you use Eclipse is to use the JAD plugin which will decompile the classes in the background. see jadclipse.sourceforge.netAmeeameer
Note that when you "create the java files" using a decompiler (or using a plugin that does this for you, ie. Eclipse's JAD plugin), the comments will NOT be present in these created .java classes. Hence when debugging, the debugger might not be able to synchronize properly (ie. it'll indicate that you are executing line 120 when line 100 is actually executed). Hence it's always better if you can get the real source code (that includes comments)Ameeameer
@oikonomopo's answer should be the accepted one. I wish I had known about that addon sooner.Dichroite
F
39

Use Java Source Attacher !

It does what eclipse should do - a right click context menu that says "Attach Java Source.

enter image description here

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.

Facetious answered 31/10, 2014 at 10:14 Comment(3)
I found a major issue with this plugin. I have a maven dependency jar file downloaded and while trying to get source for this jar ; got an error "Sorry but source code cannot be found in our database". Even after providing github repo URL ; it never downloaded the source filesDogtired
I have a maven project in eclipse STS. I don't see the attach source option there.Brigid
Just for info, It fails to attach java10 source, anyways we can attach it manually.Dialectical
T
18

Eclipse is showing no source found because there is no source available . Your jar only has the compiled classes.

You need to import the project from jar and add the Project as dependency .

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.

Thorathoracic answered 2/3, 2013 at 23:8 Comment(3)
Thanks Avinash, I tried your second option but still it says the same thing. What I did is- After unzipping the jar I will be getting a folder right? So if I point to that folder, I should be able to see the source code properly right? But it is not happening to me. I am not able to see the class file properly.Vickery
What do you mean you can't see class file properly ? Class files won't be in a readable format as they will contain bytecode. If you don't have the source files(.java) in the folder then I'm afraid your only option is to decompile your Java classes and then attach the source folder.Thorathoracic
@Nevzz03 Does the JAR file contain any .java files? Most likely it only contains .class files. Eclipse is asking you to tell it where the .java files are, though, so the JAR file is probably not helpful here.Hastate
V
16

A .jar file usually only contains the .class files, not the .java files they were compiled from. That's why eclipse is telling you it doesn't know the source code of that class.

"Attaching" the source to a JAR means telling eclipse where the source code can be found. 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).

That said, you don't necessarily need the source to debug.

Visit answered 2/3, 2013 at 23:6 Comment(8)
Thanks for the suggestion. After unzipping the jar I will be getting a folder right? So if I point to that folder, I should be able to see the source code properly right?Vickery
Only if the JAR file contains the source code (the *.java files), which most JAR files don't ...Visit
Yeah, you are right. After unzipping the jar files, I found only .class files there. So there is no way I can make this thing work right?Vickery
@Nevzz03 Do you have the .java files that were compiled into those .class files? If not, then no, you cannot attach the source files to Eclipse because you don't have them.Hastate
If you have the jar file with only .class files, you don't have the source code (the .java files). BUT you can generate it using the appropriate tool, ie. jd-gui . In jd-gui, open your jar, then go to File > Save all Source, trick is done :) see #8455090Ameeameer
Another possibility if you use Eclipse is to use the JAD plugin which will decompile the classes in the background. see jadclipse.sourceforge.netAmeeameer
Note that when you "create the java files" using a decompiler (or using a plugin that does this for you, ie. Eclipse's JAD plugin), the comments will NOT be present in these created .java classes. Hence when debugging, the debugger might not be able to synchronize properly (ie. it'll indicate that you are executing line 120 when line 100 is actually executed). Hence it's always better if you can get the real source code (that includes comments)Ameeameer
@oikonomopo's answer should be the accepted one. I wish I had known about that addon sooner.Dichroite
H
6

This worked for me for Eclipse-Luna:

  1. Right Click on the *.jar in the Referenced Libraries folder under your project, then click on 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.
Heredia answered 27/5, 2015 at 19:2 Comment(1)
As of my writing this, the process has changed. Java Build Path > Libraries > the-subject-jar-file. Notice the right caret just left of the library name (>). Click just to the right of the caret and to the left of the name. Now you will see the "Source attachment:" field.Pothead
S
2

Go back in to where you added the jar. I believe its the libraries tab, I don't have Eclipse open but that sounds right. 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. 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. I don't pay much attention when I'm in there anymore if you couldn't tell. That's what you were asking, right?

Sis answered 2/3, 2013 at 23:9 Comment(0)
W
2

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. Atttach your source code using
i.e, 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
2. Using eclipse source Analyzer
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. we can achieve it after installing the plugin, by right click on the open source jar and select the attach source option.
3. Using Jadclipse in eclipse you can do it
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. in jadclipse view you can see your .class file to decomplile source format note here you cannot see the comment and hidden things

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.

Wight answered 26/9, 2015 at 9:24 Comment(0)
P
1

Simply import the package of the required source class in your code from jar.

You can find jar's sub packages in

Eclipse -- YourProject --> Referenced libraries --> yourJars --> Packages --> Clases

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

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:

import com.mysql.jdbc.*;

Then program is working fine.

Patricia answered 6/6, 2016 at 13:18 Comment(0)
J
1

I faced the same issue and solved using the below steps. Go to Windows->preferences->Editors->File Associations

Here click on Add then type .class click on OK

again click on Add then type .classwithoughtsource click on OK

Now you will be able to see JadClipse option under Java section in Windows->Preferences

Please provide the path of jad.exe file as shown below.

Path for Decompiler-C:\Users\ahr\Documents\eclipse-jee-galileo-SR2-win32\jad.exe Directory for temporary Files-C:\Users\ahr.net.sf.jadclipse

click on Apply

Now you should be able to see the classfiles in proper format.

Jermyn answered 16/3, 2017 at 12:20 Comment(0)
W
1

It is quite possible, just go to the jar in the Build Path and choose to attach a source just like follow.

enter image description here

Wellmannered answered 18/4, 2022 at 13:19 Comment(0)
M
0
  1. Download JDEclipse from http://java-decompiler.github.io/
  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.

Drop down option location

Magnanimous answered 20/9, 2019 at 8:55 Comment(0)
D
0

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. For Example "Spring Core" » "4.0.6.RELEASE"

  2. open https://mvnrepository.com/ and search file with name "Spring Core" » "4.0.6.RELEASE".

  3. Now Maven repository will show the the details of that jar file.

  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/".

  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.

Detoxify answered 16/1, 2020 at 10:6 Comment(0)
M
0

I am using project is not Spring or spring boot based application. 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. I am using the latest version of Eclipse version 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).

Megass answered 18/3, 2020 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.