23

Eclipse 3.5 added support for relative buildpath/classpath items, yet I cannot seem to find a graphical way to add relatively pathed items. The example in new and noteworthy (search the page for 'relative', about 40% of the way down) seems to indicate that I have to manually edit the .classpath file. Am I just missing something?

Edit 2015: Updated link as it had went away. To preserve the "new and noteworthy" indefinitely, here's a screenshot of the mentioned section.

enter image description here

zb226
  • 9,586
  • 6
  • 49
  • 79
basszero
  • 29,624
  • 9
  • 57
  • 79

9 Answers9

11

The actual way to do this is to import the library into your workspace so,

Right Click in Package Explorer -> Import -> File System -> Browse (to the directory containing your jars) -> check the boxes next to the jars you want -> Browse to the workspace folder you want to import to (probably your lib folder) -> Finish

Keep in mind this will copy the files into the lib folder so I've found that it helps keep things clean if you don't already have them in there. They will now show up in the package explorer under "lib." To add them to the classpath,

Right click on the project -> Build Path -> configure build path... -> Add JARs -> navigate to the given project's lib folder and select the appropriate JARs -> click ok -> click finish

This adds them to the CP with relative locations.

If you need more help let me know

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • 4
    Not quite. I don't want to import the jars into the project, I want to refer to my lib directories (which are external and several parent dirs away) via relative paths so that anyone who checks out my projects won't have to set variables or environment – basszero Sep 16 '09 at 13:18
  • The eclipse relative build paths are project relative. As far as I know, the only way to get eclipse to actually refer to things in that manner is to have them inside the project in the project manager. You might be able to use operators like .. to refer to other directories, but I know of no way to graphically accomplish that. Sorry! – Chris Thompson Sep 16 '09 at 17:53
11

I'm uing Helios (i.e. 3.6.x) and this is what I do to reference a lib (jar) outside of my project in a relative manner.

1) Right click project in project explorer -> Import -> Import. 2) Select File System (Next) Use the browse button to navigate to the folder (yes, it looks like a hard-path). Select the .jar(s) to include.

I usually append "\lib" to the project name in the "Into Folder:" option.

Click "Advanced" and select the "Create Links in workspace" and I use the default options (both checked, and relative to PROJECT_LOC). 3) Click Finish

Next, open up the project properties and go to Java Build Path Libraries tab and select "Add Jars..." and go to the \lib folder (from Step 2) and select the lib(s) that you need. On the Libraries tab now has "ProjectName/Lib" for the path of the lib(s).

The project explorer will display a lib folder with your libs with a special icon. If you look in your hard drive for the lib folder, there is nothing there. So it is referring to the libs in the external path, but including them as a "project" lib.

Right click on the jar file in the project explorer and do properties. You should see the relative path goodness.

zb226
  • 9,586
  • 6
  • 49
  • 79
Mike Jones
  • 111
  • 1
  • 2
  • 1
    Perfect! Using this method, a virtual folder is created to contain you jar files. The jar files themselves are **not copied** but kept on their original location and referenced using a relative path (e.g. `/../../mylittle.jar`). – Daniel Jul 11 '13 at 08:10
  • Did you have to setup PROJECT_LOC or was it predefined? – Rikki Nov 25 '15 at 14:56
8

Simple. without all dialogs and menus.

In your .project file add the path to the real file, like this. Mine is on ../libs/GoogleAdMobAdsSdkAndroid-4.1.1/GoogleAdMobAdsSdk-4.1.1.jar
so i changed to PARENT-1-PROJECT_LOC/libs/...

<linkedResources>
    <link>
        <name>libs/GoogleAdMobAdsSdk-4.1.1.jar</name>
        <type>1</type>
        <locationURI>PARENT-1-PROJECT_LOC/libs/GoogleAdMobAdsSdkAndroid-4.1.1/GoogleAdMobAdsSdk-4.1.1.jar</locationURI>
    </link>
</linkedResources>

and in .classpath i use

<classpathentry kind="lib" path="libs/GoogleAdMobAdsSdk-4.1.1.jar"/>

as the link to the lib

Eclipse links the resource to project and uses it without copping the file Just like Mike Jones answer above but without any dialogs

Best for multiple apps

Jud
  • 81
  • 1
  • 1
5

This does not work!

Editing the .classpath to include relative paths as per the New and Noteworthy example (path="../../lib/myjar.jar") isn't difficult, however it does not appear to work as described!

It seems to resolve relative to the Workspace and not the specific Project as specified, which is not much use. Has anyone else found the same problem or a solution to this?

3

I think you can do this graphically instead of editing .classpath. Depending on Eclipse version it's something like:

Right-click project --> Properties --> Build Path --> Libraries (tab) --> Add Variable

Works in my current Eclipse with a Java project.. ;-)

NickDK
  • 5,159
  • 2
  • 18
  • 11
  • 1
    That is for adding variables rather than direct relative paths. Not what was asked for at all – Rich Seller Sep 15 '09 at 13:16
  • Guess I'm missing the point of your question. I thought you wanted to change the .classpath to add something like: But using the graphic interface. My bad.. – NickDK Sep 15 '09 at 13:33
  • @basszero This is exactly what you need. Some of these variables will give you the project directory which you can use and reference your jar relative to it. – Ayman Salah Oct 03 '18 at 12:58
3

After evaluating all responses and tinkering with Eclipse I can see no way to do this without manually editing the .classpath file.

Marking this answer as accepted to indicate that there is no way to do this in Eclipse 3.5. Keep fingers crossed for Eclipse 3.6 or Eclipse 4.

basszero
  • 29,624
  • 9
  • 57
  • 79
0

As far as I know you currently can only achieve this by editing the .classpath file. For this to be possible through pure 'button pushing' the browse dialogs popping up after selecting 'Add External JARs...' and 'Add External Class Folder...' would need something like a check-box to specify if you want to add the relative or the absolute path. Maybe that is not hard to add, but I haven't seen it yet.

Simon Groenewolt
  • 10,607
  • 1
  • 36
  • 64
0

No. As of yet, it is not possible. But you will be seeing it soon. Keep checking the intergration builds.

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
0

I've done this by adding the libraries to my project. So, create a folder under your project called "jars" and put all dependent jars in there.

When you configure the build path afterward, everything will be relative.

vinnybad
  • 2,102
  • 3
  • 19
  • 29