Eclipse projects have the nice features that they can link to files and folders: so instead of having the physical file, it is just a pointer to a file. This is very cool as that way I can point to shared files, or keep files in a common place referenced from projects, and so on.
As with most things in Eclipse, there is not a single way how to do things. So I’m showing in this post several ways how to link to files and folders.
Creating a New Link
Select the folder/project where to create a link to a file. Use the context menu with New > File (or the menu File > New > Other > General > File):
That opens a dialog to create a new file. I can select which project/folder I want to use for this:
Next, click on ‘Advanced’, enable ‘Link to file in the file system’ and Browse to the file you want to link to:
This will set the link to the file:
As an absolute path (C:\….) is probably not really what you want, there are Eclipse Path Variables you can use:
:idea: Note that these are Eclipse PATH variables (not Eclipse BUILD variables), see “Eclipse Build Variables“.
To use the Path Variable for the link, use the ‘Extend…’ Button and then select the file, then press OK:
This now uses a PATH variable for that link. Note that it shows as well to which file it resolves:
Press Finish, and the link will be created. Note the small ‘arrow’ in the icon (see “Icon and Label Decorators in Eclipse“) to show a linked file:
Modifying a Link
If you right-click on that linked file and select ‘Properties’ of it, you can see that it is really a Linked File, with the link information, and you can change/Edit that link any time:
As for link to files, its possible to create ‘link to folders’. It works the same way: select the folder, then use File > New > Folder:
- Use default location: this creates a normal folder.
- Virtual Folder: this does not create a physical folder, but a virtual ‘container’ where I can place links or other virtual folders. This is useful to organize links and virtual folders, without the need for a physical folder.
- Linked Folder: like linked files, this links to a folder.
:idea: The cool thing about linked (source) folders is: when I add new files to that folder where it links to, the projects with that linked folder to it will ‘see’ the extra files too, and that way new files are automatically added to the project. I do this many times, and it is like a ‘library’ folder for me: I can add a new source file to that ‘library’ folder, and every project linking to that folder will automatically have it added. :-)
Linked files and linked folders can be deleted from the project too. In that case, the destination of the link is *not* deleted, only the link:
Using Drag & Drop
As mentioned at the beginning: there are multiple ways to do the same thing in Eclipse. Instead using the top menu, or using the context menu, I can use drag & drop. To create links, I need to hold the CTRL key:
:idea: Notice that during the drag&drop with CTRL key pressed the icon gets a ‘+’ to show copy/linking mode.
When I drop the file: I get the usual choices how I want to create the link:
Drag&Drop of files and folders do not work inside Eclipse. What works as well under Windows is to drag&drop a file or folder from the Windows Explorer :-).
Links to files and folders are a cool thing in Eclipse, and they can be created with menus or even simpler with drag&drop. This is not limited to inside Eclipse: I can drag&drop from outside with the Windows Explorer and that way can link to files and folders everywhere :-)
Happy Linking :-)