Eclipse does not remember external source attachments - eclipse

Using Eclipse 4.3.1 (Kepler) on Ubuntu, I find that each time I exit and reenter Eclipse, I have to respecify my external source attachments. What is the key to making Eclipse remember them?
Open the Source Attachment Configuration dialog by clicking on the "Change Attached Source" button in the Class File Editor; or open the similar Java Source Attachment dialog by right-clicking on the containing jar in Package Explorer and selecting "Properties".
I've tried both.
In the dialog, click on the "External File" button and navigate to a jar containing sources, or click on the "External Folder" button and navigate to a directory containing .java files in subdirectories corresponding to the package hierarchy.
All these ways work until I quit Eclipse and come back in.

I have never used these parts of Eclipse; I'm using Maven to manage my dependencies and Maven will automatically attach sources - so take my answer with a grain of salt :-)
I'm not sure why Eclipse doesn't remember those settings. My current feeling is that those settings are temporary.
If you edit the source attachments in the project's properties (look for "Java Build Path"; there is a tab "Libraries"), then the changes should be saved in the file .classpath. Have a look in there.
Lastly, you can try to create a "User Library" (search for this term in the preferences dialog). Here, you can define a bundle of JARs which make up some library. After adding the code JAR, you get options to specify the source JAR.

Related

Eclipse - why cant it integrate jars in lib folder into project?

I manually copy needed jar files into my project lib folder. Then, I try to add them to build path via "configure build path option". If i choose "add jar", then the lib folder does not show up in the "to choose from" list. So, I am forced to use "add external jars" option instead. But, that option does not make the jars a part of the project even though they lie in the lib folder. After I restart eclipse 2-3 times, the jars are magically integrated into my project.
Why is eclipse behaving this way ? Why can't I do this easily ?
Please help me.
By default, Eclipse dose not detect file change which come from outside, like copy a jar file in lib folder manually.
So after you copied jar files, you have to refresh the project by pressing F5 while selecting your lib folder. I believe that's why eclipse cannot found your jar files.
If you want to solve this problem once for all, you can active "Refresh using native hooks or polling" via
Window -> Preference -> General -> Workspace
However, this might slow down Eclipse if you have a big project with many many files.
UPDATE
As Bananeweizen mentioned, instead of doing all the copied from outside of Eclipse, you can also copied those file into Eclipse, Package Explorer View for example. This way Eclipse will detect and refresh folder automatically.
The way I was taught to add external jars to an eclipse project is to drag the jar file from explorer into the lib folder in eclipse and then on the dialogue eclipse responds with is choose the copy to option and eclipse imports the jar file and copies it to the lib location at the same time.

.class file opens instead of .java while debugging

Current setup:
MainProject which is a Library Project
BranchProject which is a new projects and has MainProject as a Reference
Whenever I debug and a file from MainProject is on focus (actually BranchProject has only graphic and xml layout changes) the Debug window opens a .class file which is read only. I want it to open the .java file so I can edit it directly.
Skyler's answer from this post worked for me:
Opening source code from debug view edits .class after Android R18 update
Here is a summary:
The fix is to right click the Project name in the debug view, and select "Edit Source Lookup..." from the menu. From there, remove the Default lookup path. After that, manually add the associated projects (not jars) that your project references. This is done by clicking Add, selecting Java Project, then checking the appropriate projects.
When you're using a Library project one of the things you're in fact doing is compiling your Library project into a jar and then referencing that jar in your calling Project.
If you right click the Project, and select "Configure Build Path" you'll see a tab called "Libraries", if you look inside "Android Dependencies" you'll notice a list of jar's corresponding to your Library projects.
These jars are expandable, showing you that they have a slot for a source attachment. Usually this would be editable allowing you to directly link the source but in terms of ADT these are already filled and are uneditable.
When debugging these files you're linked to a read-only class file with this attached source. This is because you're not running against source files directly, you're running against a pre-compiled class file. Until the ADT team get this functionality in place, you're pretty much forced to jump to the direct source code and rebuild everything.
EDIT
See #Steven linked answer :)
I faced the same issue while debugging the a .java file using Eclipse IDE. As per my understanding this issue comes when we put the xyz.class file of xyz.java file or JAR at the project build path. Delete the .class or JAR file from the project class path and rerun .java file in the debug mode. This time you see a source not found window. Click on "Source not found" button and check "Find duplicates..." at the bottom of the window. Done your problem is solved :)
The problem is that the class file is preferred over the java (by default), here is how you can change that for Eclipse (tested on NEON 2):
Right-click on the Project in the Project-Explorer, click Properties
On the new window select: Run/Debug Settings
Create a new configuration (or duplicate another one)
Select the new config and click Edit...
Go to the tab Source
Select the Default and Remove
Create a new path with Add..., select Java Library, then JRE System Library
Create a new path with Add..., select the location where the sourcecode is by Workspace folder (if it is a project in the same workspace) or File System directory (it it is not)
I think this depends on, how you set up the dependency in eclipse. You should set up your BranchProject to depend on the source-Files of your MainProject. If you depend on compiles Class-Files is obvious that the debugger opens the class files, because it does not know about the source files.
I found a good solution for me here:
Using Android library in eclipse and jumping to class files instead of source file that is within eclipse workspace
Simply, select each library project your project depends on, and use Top or Up to move it above the projects outputs. Eg. move all library projects to the top.
Open main project properties -> Java Build Path -> Projects tab and add there projects the main project depend on.
Switch to Order and Export tab and uncheck Android Dependencies
Enjoy
If you tried all above hints and it still doesn't work try this solution, it worked form me:
Right-click on the Project in the Package-Explorer, click Build Path -> Configure Build Path...
Select tab Order and Export
select library that you can't reach code and then click on button Bottom
Then click on Apply and Close
hope this can help you
Most of the time it happens when specific source folder are not added in build path Sources tab.
Right-click on the Project in the Package-Explorer, click Build Path -> Configure Build Path -> Source Tab
Add the source folder if your project source folder is not there.
Select Add folder -> select your project source folder specifically. Eg: project_name/src . Then Apply it and restart server.

Hadoop Documentation for Eclipse

I recently installed Hadoop and am able to run simple programs.
However I would like to view documentation for Hadoop classes within Javadoc browser in Eclipse.
Please let me know how to enable that (I am a little novice with Eclipse IDE).
Thanks.
A couple of suggestions:
If you're using maven for your dependency management, you should be able to expand out the list of Maven dependencies in your Eclipse project, right click the hadoop-core-x.x.x.jar and select Maven -> Download Javadocs
Otherwise you'll need to source the Javadocs from the $HADOOP_HOME/docs/api folder and associated with your hadoop-core-x.x.x.jar in Eclipse. In Eclipse, right click your Java project and select Build Path -> Configure Build Path. Now click the Libraries tab and locate the entry for hadoop-core-x.x.x.jar. Expand the entry to show options for Source, Javadoc etc locations and click the Javadoc location entry. Now click the Edit button to the right and enter the location as the path $HADOOP_HOME/docs/api in the Javadoc URL text box (mine is file:/opt/hadoop/hadoop-1.0.2/docs/api/)
Either way now you should be able to hover over Hadoop classes in Eclipse and the Javadoc will popup. You can also show the Javadoc window that will populate for the class you are currently viewing, or just open an internal browser window and point it at the $HADOOP_HOME/docs/api/index.html file
Another easy solution for those who want to stay updated is to edit the Javadocs path like Chris said:
"In Eclipse, right click your Java project and select Build Path -> Configure Build Path. Now click the Libraries tab and locate the entry for hadoop-core-x.x.x.jar. Expand the entry to show options for Source, Javadoc etc locations and click the Javadoc location entry. Now click the Edit button to the right and enter the location as the path"
but instead of linking it directly to the api you have stored on your hard disk, link it to http://hadoop.apache.org/docs/stable/api/

Eclipse - How to give dependency between projects?

I have two java projects in eclipse. Second one is using first one's jar. When I try to navigate to first one's class from second one, it opens .class of that file. But I want to open the first one project file. Please help.
Thanks in advance.
If you want to link your second project with your first project, don't add the first project in form of a JAR file to the second.
Instead open the project properties of the second project and select "Java Build Path". On the right side on the "Projects" tab you can add your first project as "required project on the build path".
The class files of project 1 are now added to the class path of project 2. And if you click on a class name you directly get to the source code.
Ideally, the JAR of the first project would appear under the "Referenced Libraries" of the second project.
Right-click on that JAR, and choose Properties -> Java Source Attachment.
Provide a variable that links to the source files of this JAR.
Alternatively, install a decompiler plugin (see jd-eclipse) which will decompile class files when you navigate them in Eclipse, so that you can see the source.
Make one project in Eclipse depend on another so that the dependencies are available in the indexer
Tested on Eclipse IDE for C/C++ Developers 2022-09 (4.25.0) on Linux Ubuntu 18.04.
For C++, right-click on the project which depends on another project. Go to "Properties" --> Project References --> check the box next to the project whose files and resources you'd like to be available in the project you are editing --> click "Apply and Close".
The indexed resources in the project next to the box you just checked (glib in the screenshot below) are now available to the project whose properties you just edited! This means if you Ctrl + Click a variable which is defined in the project you are editing, your indexer will now jump to its definition even if it lies in the referenced project (glib in this case)!
Screenshot:
Done!
Old and wrong answer (but may be helpful for adding includes to your project)
For C++, right-click on the project which depends on another project. Go to "Properties" --> C/C++ Include Paths and Symbols --> click "Add Include Path from Workspace..." --> choose the project from your workspace which it depends on, and drill down into the folder of interest --> click "OK". Click "Apply and Close".
You've now made your project whose properties you just edited depend on the other project in such a way that the dependent project dir you just added will now be indexed!

Is there an equivalent of "Add existing file" (to project) in Eclipse?

I am trying to map my Visual Studio experience onto Eclipse. Right now, I wish to add a file existing somewhere on the file system into a project in Eclipse. I expect it to be copied to the project source folder.
Currently, I see only one way - switch to the windows explorer (total commander, in my case) and copy the file myself, after which to refresh the project.
Can I do the same without leaving the Eclipse IDE?
Thanks.
I had the same problem, but found yet another solution.
Let me state my problem so that we're sure we're talking about the same thing.
I had a file in a directory where a bunch of code files were and some of the files in that directory were shown as "in my project" and some of the files were missing. I wanted the missing files to appear in my project.
I simply selected the folder in my project and pressed 'F5' or right-clicked and selected 'refresh' and the files appeared in my project. It looks like that is the intended way of doing this.
File->Import->General->File System should do it.
There you can select the file(s) from some file system folder to your project (into folder).
Even easier. Just drag and drop the file from explorer into the correct package in Eclipse. It will ask you if you want to copy or link the file(s).
I am a newbie to Eclipse Luna (the 64 bit latest version on Windows 7) but this worked for me:
Copy the preexisting source files you which add to your project.
In Project Explorer, right click your project and select New > File
In the "New File" dialog box, your project's name / folder should be displayed as the parent folder for your new (existing) source file.
Click on the "Advanced" button at the bottom of the "New File" dialog box.
Check the "Link to file in the file system" checkbox.
Click the "Browse" and browse to your preexisting source.
Click the "Finish" button at the bottom of the dialog box.
In my case, I had to:
right-click the project's name
choose "Close Project"
right-click the (now closed) project's name again
choose "Open Project"
Refreshing didn't work for me. :(
Hopefully this helps someone else...
Just found the quickest way to add files:
Copy-paste your file into your project disk directory using your file browser.
In Eclipse project browser select your project.
Click [right mouse button]->Refresh (or press F5 on Windows machine).
Voila
(worked on Neon-4.6.2)
I just got this to work. YMMV
I don't suppose it has to be there, but I put my file, xyz.cpp, into my project's source directory. My project has a source subdir named src, as in ProjName/src.
Right Click on project name (my project was open and the only one open).
NEW->FILE. Put in the filename, xyz.cpp. Click OK or press enter.
The file will open in the editor and complain that it is empty. Close it WITHOUT SAVING.
Reopen the file. It now has its contents.
It turns out that just simply dragging the files into the src folder would work!
Right-click on the file after you have copied it into the project directory. Select Properties... in the left hand pane select C/C++ Build. There will be a checkbox "Exclude resource from build" uncheck that check box.