51

I imported external libraries using absolute path. But I have two work environments, switching between Linux and Windows. Projects are downloaded from SVN. So I was wondering whether I can import these libraries by relative path.

jkdev
  • 11,360
  • 15
  • 54
  • 77
ablimit
  • 2,301
  • 6
  • 27
  • 41

5 Answers5

49

You should declare a variable (Java Build Path -> Add Variable... -> Configure Variable ... -> New) to set the changing path on each system (e.g. FOO_BAR_HOME).

Than you can add the Variable to the Libraries and edit it to point to your library, like

%FOO_BAR_HOME%/lib/foobar.jar

Take a look at the existing variables for usage.

Alternative you can place the library inside the project (e.g. subfolder 'lib'). If you add the library from this location ('Add Jars...' NOT 'Add External Jars...') it will be added by relative path.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • 7
    Thanks Claszen. I used the "add jars" instead of "external jars". It works. – ablimit Feb 13 '11 at 15:18
  • 2
    I need to use the variable idea, but I can't figure out how to use it. When I click Edit... on one of my external libraries, I get a Finder window (I'm on OSX Lion) that allows me to navigate to my jars, but nothing that allows me to edit that path to use my variable instead. Any ideas? – SilithCrowe Feb 20 '12 at 17:16
  • @SilithCrowe Click `Add Variable...` and select the variable you want to use (or configure a new one using `Configure Variables...`) - after inserting the variable `Edit...` it (add the refrence to your library to the path given by the variable). – FrVaBe Feb 21 '12 at 08:30
  • @FrVaBe Thank you, that works. I guess I was trying to add the variable to existing paths, but instead it seems you need to *start* with the variable and then add the relative path from there. The workflow is a little funky with this feature, I think. – SilithCrowe Feb 22 '12 at 14:57
  • 2
    @FrVaBe Also, I made a discovery: In addition to adding and then editing a variable, you can also click "Add Variable..." and then "Extend..." to add a library with one fewer step (I know, who cares, right? I'm adding long lists of libraries though, so it helps a little. :P ) – SilithCrowe Feb 22 '12 at 15:00
  • @SilithCrowe _Who cares?_ All people who benefit from this workflow optimization - so thank you for sharing it! – FrVaBe Feb 22 '12 at 16:30
  • Is this exclusive to the JDT or it's just me? (I'm trying to add a variable for a pydev-based project.) – n611x007 Jan 18 '13 at 14:37
  • Is it possible to use variables to define a user library? – Danny Lo Oct 30 '14 at 16:16
  • @DannyLo I do not know how. It looks like you can only add concrete JARs to a User Library. – FrVaBe Nov 03 '14 at 11:42
  • Then I suppose I'm gonna use maven :)P – Danny Lo Nov 03 '14 at 15:14
  • 1
    @FrVaBe I am able to add Vairable, but not edit the path of external jars. How do I make it something like : `%FOO_BAR_HOME%/lib/ACTIVEMQ.jar` – SGuru Jun 23 '16 at 19:29
  • 1
    I had the same problem, working in Windows and Eclipse. I tried to manually edit the .classpath file, but in Build Path it a) kept concatenating the project folder with the path b) didn't even resolve the value of the variable and used %FOO_BAR_HOME% in the concatenated string. I finally got to make it work by following the way described here http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java) by expanding the variable and selecting the jars. The result is a kind="var" path="FOO_BAR_HOME" line, and several similar variable lines below it – fyts Mar 09 '17 at 13:01
9

Hey I was having this issue as well, but I can offer an alternate solution for those who want to keep the required library locally but outside the directory of the Java Project.

First, add the library you want using the usual "Add External Library/Jar" method.

If you go into the home directory of your Java Project, you should see a .classpath file. Edit this with any text editor you wish, and look for the line that references the library you added. Then, say your library is called lib.jar and you have it one level outside of your project.

Lets say the line says:

<classpathentry kind="lib" path="C:/Users/Public/workspace/lib.jar"/>

Rather than moving lib.jar to your Project's directory, you can just change this line to say:

<classpathentry kind="lib" path="./../lib.jar"/>

and your Java Build path will be updated to look for it in the directory one level above the current project.

Also, to my knowledge if you're running a Mac or any Unix based OS, then the .classpath file will be hidden. In order to access it, you can try opening it from Terminal, by navigating to the directory that your Java Project uses (the actual project folder itself, not the workspace), then either vi or vim ".classpath". Keep in mind that using ls or even ls -a might not work, but trust that it will be there.

Once you've changed it to the specified location, save the file and when you go back into eclipse, the reference should still be working. Keep in mind that the "./" at the beginning is necessary. Just having "../" will not work, or at least it didn't for me.

aljo
  • 91
  • 1
  • 2
  • Having 8 entries in my .classpath all relative and above the main folder of the project, when using "../" at the beginning only some work. Using "./../" at the beginning makes all of them work. very confusing. thanks for the tip @aljo – thedrs Feb 07 '22 at 06:44
4

I did it very simple. I had a lib with an absolute path in my classpath:

/home/glauco/workspace/ltClubs/lib/swingx-core-1.6.2.jar

So i just removed the absolute path from it and it works. Now it's relative xD:

lib/swingx-core-1.6.2.jar
JMax
  • 26,109
  • 12
  • 69
  • 88
  • This works for me when I build with `ant` command. But when I run it from eclipe I get an error saying that it can't find libs – Uko Sep 24 '12 at 13:43
3

This should be a comment on the previous answer, but the strange reputation system from this site forces me to post a new answer instead... (no comments)

You can use a relative path, but you're missing the './' in it.

Instead of

lib/swingx-core-1.6.2.jar

you should use

./lib/swingx-core-1.6.2.jar
Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
luispablo
  • 83
  • 1
  • 9
0

Add the folder containing the dependencies into the JAVA project. Select and right-click the dependencies, "Add to Build Path" pops up as a context menu. This adds the dependencies using relative path instead of the absolute path.