How can I link source to a jar package in eclipse?
Asked Answered
M

4

12

How can I link source to a jar package in eclipse?

I am trying to add the external library ch.ntb.usb.

I added the jar file to my build path, but when I tried to run the application it returned the following error:

The jar file ch.ntb.usb has no source attachment.

I've used JD-GUI to decompile the jar file and the source code is contained.

Methodist answered 10/12, 2011 at 5:16 Comment(5)
a) You needn't decompile a jar file, if it contains the source, to get the source. Do you mean 'unzip(:= jar -xf FILE.jar)' with 'decompile'? b) You don't need the source to run a program using a jar - but the IDE might need it for tooltips, containing the help, or for debugging (stepping into the code).Pentecost
Yeah maybe I meant unzip....anyway I "Saved all sources" with jd-gui into my build path as ch.ntb.usb.src after unzipping the jar file. Then I set the source attachment of the external library to ch.ntb.usb.src. This fixed it. Thanks for the response/input.Methodist
Make this a self-answer. It might look a bit fishy, but it is really beneficial for others to know, that your question got a valid, useful answer. Finding, and more so, coming back and reporting it, means, you deserve it, and it is used practice (search meta, if you don't believe me - maybe the faq).Pentecost
related #15180911Engler
related #371314Engler
M
4

I "Saved all sources" with jd-gui into my build path as ch.ntb.usb.src after unzipping the jar file. Then I set the source attachment of the external library to ch.ntb.usb.src. This fixed the issue.

Methodist answered 3/12, 2012 at 21:55 Comment(2)
For clarification: sometimes only the .class files are present in a jar file. So you'd need to get the source code somehow, one way is to get it from whoever provided the relevant library (ie. on their SVN or GIT source code repo). The real trick you did here (not sure if you realized you did that :] ) is that when doing "save all source" on jd-gui, it actually created the java files for you :)Engler
Note that when you "create the java files" using a decompiler, 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)Engler
D
9

I doubt that you get that error when you only run the project unless you have an error in your code and eclipse debugger try to show the line of code that cause the exception, anyway you may need source files of a library for debugging purpose, to link source to a jar package in eclipse you can follow these simple steps :

  • Find the package which you want to add the source under Referenced Libraries in Package Explorer
  • Right Click on the library
  • Select Properties
  • Go under Java Source Attachment
  • Select the directory or archive where your source is located.

example

Defunct answered 10/12, 2011 at 8:43 Comment(2)
Thanks for this , after following instructions. I've cloned the repo and added. But Open Declaration takes me to .class file instead of .java . can you please clarify ?Georgetown
Thanks man. I have been looking for this for like an hour or so!Flourishing
M
4

I "Saved all sources" with jd-gui into my build path as ch.ntb.usb.src after unzipping the jar file. Then I set the source attachment of the external library to ch.ntb.usb.src. This fixed the issue.

Methodist answered 3/12, 2012 at 21:55 Comment(2)
For clarification: sometimes only the .class files are present in a jar file. So you'd need to get the source code somehow, one way is to get it from whoever provided the relevant library (ie. on their SVN or GIT source code repo). The real trick you did here (not sure if you realized you did that :] ) is that when doing "save all source" on jd-gui, it actually created the java files for you :)Engler
Note that when you "create the java files" using a decompiler, 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)Engler
I
0

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

Implosive answered 26/9, 2015 at 9:27 Comment(0)
B
0
  1. Click right in Package Explorer your Project
  2. Select Properties
  3. Select Android
  4. Scroll down
  5. Under Library click Add
  6. Select the needed Lib and press OK
Beedon answered 26/10, 2015 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.