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.

Sean Greevers
  • 235
  • 1
  • 5
  • 14
  • 1
    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). – user unknown Dec 10 '11 at 05:58
  • 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. – Sean Greevers Dec 10 '11 at 06:07
  • 1
    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). – user unknown Dec 10 '11 at 06:38
  • related http://stackoverflow.com/questions/15180411/attach-the-source-in-eclipse-of-a-jar/15180476#comment28567567_15180476 – Adriano Oct 10 '13 at 10:53
  • related http://stackoverflow.com/questions/370814/how-to-set-a-breakpoint-in-eclipse-in-a-third-party-library – Adriano Oct 14 '13 at 09:13

4 Answers4

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

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 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 ? – vikramvi Jul 14 '16 at 10:40
  • Thanks man. I have been looking for this for like an hour or so! – joker Aug 10 '20 at 16:25
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.

Sean Greevers
  • 235
  • 1
  • 5
  • 14
  • 1
    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 :) – Adriano Oct 10 '13 at 09:33
  • 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) – Adriano Oct 14 '13 at 08:39
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

ramesh027
  • 796
  • 1
  • 10
  • 27
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
Ingo
  • 5,239
  • 1
  • 30
  • 24